/****************************** (C) COPYRIGHT 2016 Rsestudio ****************************
* File Name          : 
* Author             : RSES_Liyang
* Version            : V1.0.0
* Date               : 00-May-20
* Description        : 
*****************************************************************************************/
//update list
//V3.5.6B       优化Power_off 函数。处理多次按键导致的无法关机问题
//V4.0.0C       优化测温部分逻辑到循环读取
//V4.0.0D       增加EFLOG日志功能
//V4.0.0E       RPT增加角度计算功能
//V4.0.0F       增加顶部ICON USB BT 采样间隔显示
//V4.0.0G       完善LOG记录基本功能。添加开机LOG记录   
//V4.0.1A       增加新版硬件支持——第一版
//V4.0.1B       修改atan2为32位，姿态显示精度提升到0.01度
//V4.0.1C                 取消屏幕刷新时的互斥锁，互斥锁会导致定时器锁死。留待以后处理

//V5.0.0A                 ST 版本

//V5.0.2A                  修改震动传感器算法，设备内部计算震动数值。修改数据存储逻辑，删除原有10条震动记录，改用一条记录存储多样数据
//V5.0.2B     20250616     修改氧含量算法，优化数值极小时变为最大的BUG

//V502D       20250708     初步适配新版MINI，增加负温度测量

//V502E       20250807     合并WIFI功能，优化温度测量时不拉高CS会影响振动的问题
//V502F       20250903     修复设备自动关机后仍然放电故障，优化WIFI启动时初始化问题

/* Includes ----------------------------------------------------------------------------*/
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "nvs_flash.h"      // 非易失性存储
// #include "esp_system.h"		// esp_restart
#include "esp_log.h"
#include "sys/time.h"		// settimeofday
#include "esp_rtc_time.h"
#include "esp_log.h"

#include "UI.h"
#include "Bsp.h"
#include "main.h"
#include "device_pins.h"
#include "MEAS.h"
#include "W25X.h"
#include "ComModuleUSB.h"
#include "wifi_config.h"
// #include "W2564.h"


int32_t cb_update_0x3d(frame_head_t *p_cmd_head);

/* Extern variables --------------------------------------------------------------------*/
iProfile_t iProfile;

uint8_t v_data_table[16] = {V_DATA_LIST};

/* Private define ----------------------------------------------------------------------*/
/* Private macro -----------------------------------------------------------------------*/
	
/* Private typedef ---------------------------------------------------------------------*/
/* Private variables -------------------------------------------------------------------*/

uint8_t Reset_system_status;
uint8_t IntoBootMode_flag;

TaskHandle_t iprofile_thread = NULL; 		/* 任务句柄 */  //struct rt_thread iprofile_thread;
StaticTask_t iprofile_thread_tcb;   	
StackType_t iprofile_stack[2048];		/* 任务栈空间 */

uint8_t simpleProfile_RealTime_Buf[256] = {0};

/* Private functions -------------------------------------------------------------------*/
static file_info_t FileInfo;
int NVM_SetIntoBootMode(void);

extern RTC_DATA_ATTR uint32_t RTC_BKP;
extern void del_group_file_id(uint32_t grp);

//信号量句柄
SemaphoreHandle_t sem_iprofile = NULL; //struct rt_semaphore sem_iprofile;
StaticSemaphore_t xsem_iprofile;  // 存储互斥量的数据结构

// static struct rt_timer timer_width_poll;
void meas_params_copy(MEAS_PARAMS *psrc,MEAS_PARAMS *pdst)
{
    pdst->SamplingSpan  = psrc->SamplingSpan;
    pdst->Start_Cond    = psrc->Start_Cond;
    pdst->Start_Params_1= psrc->Start_Params_1;
    pdst->Start_Params_2= psrc->Start_Params_2;
    pdst->Start_Params_3= psrc->Start_Params_3;
    pdst->Start_Params_4= psrc->Start_Params_4;
    pdst->Stop_Cond     = psrc->Stop_Cond;
    pdst->Stop_Params_1 = psrc->Stop_Params_1;
    pdst->Stop_Params_2 = psrc->Stop_Params_2;   
    pdst->Stop_Params_3 = psrc->Stop_Params_3;
    pdst->Stop_Params_4 = psrc->Stop_Params_4;
}


/*******************************************************************************
* Function Name  : cb_reset_device
* Description    : 
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
int32_t cb_reset_device(frame_head_t *p_cmd_head)
{	
	if(p_cmd_head->DataLength == 4)
	{
		if(	(p_cmd_head->pDataBuf[0]==0xAA)&&
			(p_cmd_head->pDataBuf[1]==0xCC)&&
			(p_cmd_head->pDataBuf[2]==0x55)&&
			(p_cmd_head->pDataBuf[3]==0xBB))
		{
			Reset_system_status = 1;
			p_cmd_head->DataLength =0;
			p_cmd_head->DeviceStatus = CMD_SUCCESS;
			
			xSemaphoreGive(sem_iprofile);   //rt_sem_release(&sem_iprofile);			
		}
		else if((p_cmd_head->pDataBuf[0]==0x33)&&
			(p_cmd_head->pDataBuf[1]==0x99)&&
			(p_cmd_head->pDataBuf[2]==0x77)&&
			(p_cmd_head->pDataBuf[3]==0x66))
		{
			IntoBootMode_flag = 1;
			p_cmd_head->DataLength =0;
			p_cmd_head->DeviceStatus = CMD_SUCCESS;
			
			xSemaphoreGive(sem_iprofile);   //rt_sem_release(&sem_iprofile);		
		}
		else
		{
			p_cmd_head->DataLength =0;
			p_cmd_head->DeviceStatus = CMD_INVPARAMS;
		}		
	}
	else
	{
		p_cmd_head->DataLength =0;
		p_cmd_head->DeviceStatus = CMD_INVPARAMS;
	}
	return 1;
}


//	uint16_t Flash_If_Erase(uint32_t Sector,uint16_t Count);
//	uint16_t Flash_If_Write(uint8_t * src, uint32_t dest, uint32_t Len);



/*******************************************************************************
* Function Name  : dpoint_cal
* Description    : 
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
int32_t cb_restore_factory(frame_head_t *p_cmd_head)
{	
	if(p_cmd_head->DataLength == 4)
	{
		if(	(p_cmd_head->pDataBuf[0]==0xAA)&&
			(p_cmd_head->pDataBuf[1]==0xCC)&&
			(p_cmd_head->pDataBuf[2]==0x55)&&
			(p_cmd_head->pDataBuf[3]==0xBB))
		{
			Reset_system_status = 2;
			p_cmd_head->DataLength =0;
			p_cmd_head->DeviceStatus = CMD_SUCCESS;
			
			xSemaphoreGive(sem_iprofile);   //rt_sem_release(&sem_iprofile);			
		}else
		{
			p_cmd_head->DataLength =0;
			p_cmd_head->DeviceStatus = CMD_INVPARAMS;
		}		
	}
	else
	{
		p_cmd_head->DataLength =0;
		p_cmd_head->DeviceStatus = CMD_INVPARAMS;
	}
	return 1;
}

/*****************************************************************************************
* Function Name  : cb_get_device_id
* Description    : 
* Input          : None
* Output         : None
* Return         : None
*****************************************************************************************/
int32_t cb_get_device_id(frame_head_t *p_cmd_head)
{
	uint8_t i;
	  
	for(i=0;i<16;i++)
	{
		p_cmd_head->pDataBuf[i] = DEV_UID[i];	
	}
	p_cmd_head->DataLength = 16;
	p_cmd_head->DeviceStatus = CMD_SUCCESS;
	return 0;
}

/*****************************************************************************************
* Function Name  : cb_get_WIFI_Firmware
* Description    : 
* Input          : None
* Output         : None
* Return         : None
*****************************************************************************************/
int32_t cb_get_WIFI_Firmware(frame_head_t *p_cmd_head)
{
	uint8_t i;
	
	for(i=0;i<29;i++)
	{
		p_cmd_head->pDataBuf[i] = WIFI_FirmwareVerBuf[i];	
	}
	
	p_cmd_head->DataLength = 29;
	
	p_cmd_head->DeviceStatus = CMD_SUCCESS;
	return 0;
}

/*****************************************************************************************
* Function Name  : cb_get_process_data
* Description    : 
* Input          : None
* Output         : None
* Return         : None
*****************************************************************************************/
int32_t cb_get_process_data(frame_head_t *p_cmd_head)
{
	uint8_t i;
	int16_t *pwBuf;
	int32_t *pdwBuf;

	static uint8_t frame_id;
	
	if(frame_id >=3)
		frame_id=0;
  
	p_cmd_head->pDataBuf[0] = 0xAA;
	p_cmd_head->pDataBuf[1] = frame_id;
	p_cmd_head->pDataBuf[2] = 0;//32;
	p_cmd_head->pDataBuf[3] = 0;
	
	if(frame_id ==0)
	{
		//4~5
		p_cmd_head->pDataBuf[6] = iProfile.Ble.Rssi;
		p_cmd_head->pDataBuf[7] = 1;//iProfile.Status.BoardConnect;
		p_cmd_head->pDataBuf[8] = iProfile.sys_status.BatChargeStatus;
		p_cmd_head->pDataBuf[9] = iProfile.sys_status.BatLevelStatus;
		p_cmd_head->pDataBuf[10] = iProfile.sys_status.Date.year;
		p_cmd_head->pDataBuf[11] = iProfile.sys_status.Date.Month;
		p_cmd_head->pDataBuf[12] = iProfile.sys_status.Date.Day;
		p_cmd_head->pDataBuf[13] = iProfile.sys_status.Date.Hour;
		p_cmd_head->pDataBuf[14] = iProfile.sys_status.Date.Minute;
		p_cmd_head->pDataBuf[15] = iProfile.sys_status.Date.Second;

		*(uint16_t*)(&p_cmd_head->pDataBuf[16]) = iProfile.sys_status.BatVoltage;
		*(uint16_t*)(&p_cmd_head->pDataBuf[18]) = iProfile.sys_status.BatTempetature;
		*(uint16_t*)(&p_cmd_head->pDataBuf[22]) = iProfile.temperature.ColdTemper;

		pwBuf = (int16_t*)&p_cmd_head->pDataBuf[24];
		for(i=0;i<10;i++)
		{
			pwBuf[i] = iProfile.temperature.temperature[i];
		}

		p_cmd_head->pDataBuf[44] = iProfile.cmd_ctrl.Status;//NONE = 0,IDLE = 1,COND = 2,RUNING = 3,PAUSE = 4

		pdwBuf = (int32_t*)&p_cmd_head->pDataBuf[48];
		*pdwBuf = iProfile.cmd_ctrl.Recorder_cnt;

		pwBuf = (int16_t*)&p_cmd_head->pDataBuf[52];
		*pwBuf = iProfile.sys_status.MeasFileID;

		p_cmd_head->DataLength = 54;
		p_cmd_head->DeviceStatus = CMD_SUCCESS;
    
	}
	else if(frame_id == 1)
	{
		pwBuf = (int16_t*)&p_cmd_head->pDataBuf[6];
		i=0;

		for(i=0;i<22;i++)
		{
			pwBuf[i] = iProfile.temperature.temperature[10+i];
		}

		p_cmd_head->DataLength = 58;
		p_cmd_head->DeviceStatus = CMD_SUCCESS;
	}
    else if(frame_id == 2)
    {
        pdwBuf = (int32_t*)&p_cmd_head->pDataBuf[8];
		i=0;

		pdwBuf[i++] = iProfile.vibrate.vib_x;      
        pdwBuf[i++] = iProfile.vibrate.vib_y;       
        pdwBuf[i++] = iProfile.vibrate.vib_z;       
        pdwBuf[i++] = iProfile.speed.s1_value;
        pdwBuf[i++] = iProfile.speed.s2_value;
		
        pdwBuf[i++] = iProfile.oxygen.oxygen_value; 
//---------------------- 8         
		pdwBuf[i++] = iProfile.pressure.pressure_data;
        
        pdwBuf[i++] = iProfile.r_width.relative;
        
        pdwBuf[i++] = (iProfile.vibrate.pitch<<16)|((iProfile.vibrate.roll<<0)&0xFFFF);
        
        //12
        pdwBuf[i++] = iProfile.conf.over_temp_stop;
        pdwBuf[i++] = (meas_debug[0]<<0)|(meas_debug[1]<<16);
        pdwBuf[i++] = (meas_debug[2]<<0)|(meas_debug[3]<<16);
        //pdwBuf[i++] = (meas_debug[4]<<0)|(meas_debug[5]<<16);
        //pdwBuf[i++] = (meas_debug[6]<<0)|(meas_debug[7]<<16);

		p_cmd_head->DataLength = 58;
		p_cmd_head->DeviceStatus = CMD_SUCCESS;
    }
	else 
	{
		pwBuf = (int16_t*)&p_cmd_head->pDataBuf[6];
		i=0;
		pwBuf[0] = 0;
		
		p_cmd_head->DataLength = 58;
		p_cmd_head->DeviceStatus = CMD_SUCCESS;	
	}
	
	frame_id++;
	
	return 0;
}

/*****************************************************************************************
* Function Name  : cb_get_flash_capacity
* Description    : 
* Input          : None
* Output         : None
* Return         : None
*****************************************************************************************/
int32_t cb_get_flash_capacity(frame_head_t *p_cmd_head)
{
  
	//uint8_t i;
	switch(p_cmd_head->Proces)
	{
		case 0:
		{		
			p_cmd_head->pDataBuf[0]=(uint8_t)GetGroupFile_use();
			p_cmd_head->pDataBuf[1]=DATA_RECODER_TOTAL_ITEM;
			p_cmd_head->DataLength =2;
			p_cmd_head->DeviceStatus = CMD_SUCCESS;
		}break;
		
		
		default:
		{
			p_cmd_head->DataLength =0;
			p_cmd_head->DeviceStatus = CMD_INVPARAMS;
		}break;
	}
	
	return 0;
}

/*****************************************************************************************
* Function Name  : led_Polling
* Description    : 
* Input          : None
* Output         : None
* Return         : None
*****************************************************************************************/
int32_t fw_update_count;
int32_t fw_update_size;
int32_t cb_upload_fwfile(frame_head_t *p_cmd_head)
{

	switch(p_cmd_head->Proces)
	{
		case 0:
		{
			fw_update_count = 0;
			fw_update_size = 	((uint32_t)p_cmd_head->pDataBuf[3]<<24) | 
								((uint32_t)p_cmd_head->pDataBuf[2]<<16) | 
								((uint32_t)p_cmd_head->pDataBuf[1]<<8) | 
								((uint32_t)p_cmd_head->pDataBuf[0]<<0);
		}break;
		
		case 1:
		{
			if(fw_flash_write(fw_update_count*32,p_cmd_head->pDataBuf,32)!=0)
			{
				p_cmd_head->DataLength =0;
				p_cmd_head->DeviceStatus = CMD_ERROR;
			}
			else
			{
				fw_update_count++;
				p_cmd_head->DataLength=0;
				p_cmd_head->DeviceStatus = CMD_SUCCESS;
			}
		}break;
		
		case 2:
		{
			fw_update_count = 0;
			
			p_cmd_head->DataLength=0;
			p_cmd_head->DeviceStatus = CMD_SUCCESS;	
		}break;
		
		case 3:
		{
			W25X_Read_Addr(FW_MEM_ADDR + fw_update_count*32,p_cmd_head->pDataBuf,32);
			fw_update_count++;
			p_cmd_head->DataLength=32;
			p_cmd_head->DeviceStatus = CMD_SUCCESS;		
		}break;
		
		case 4:
		{
			fw_flash_write_success(fw_update_size);
			p_cmd_head->DataLength=0;
			p_cmd_head->DeviceStatus = CMD_SUCCESS;	
		}break;
		
		
		default:
		{
			p_cmd_head->DeviceStatus = CMD_UNSUPPORT;
			p_cmd_head->DataLength = 0;
		}break;		
	}
	
	return 0;
}

/*****************************************************************************************
* Function Name  : cb_op_thermo_calib
* Description    : 
* Input          : None
* Output         : None
* Return         : None
*****************************************************************************************/
int32_t cb_op_thermo_calib(frame_head_t *p_cmd_head)
{
	uint8_t channel;
	uint8_t mode;
	uint8_t params;
	int16_t temp_16 = 0;
	int32_t raw_low;
	int32_t raw_high;
	int32_t real_low;
	int32_t real_high;
	int32_t offset;
    
    
    (void)params;
    
	//byte 1 channel
	//0 inner temperature
	//1 res
	//5 cold temperature calib
	//10 - 127 temperaturechannel calib
	//130 pressure A calib
	//131 pressure B calib
	//135 oxy A calib
	//136 oxy B calib
	//140 dist A calib
	//141 dist B calib
    //145 angle_pitch
    //146 angle_roll
	
	//byte 2,3 Mode - params
	//0:stand offset 16bit +-32768
	//5:stand 2point
	//10:stand 2point only b (offset)
	//15:stand multi2point
	
	//5:stand 2point
	//Byte 4 - 7 	raw_low 
	//Byte 8 - 11 	raw_high
	//Byte 12 - 15	real_low
	//Byte 16 - 19  real_high
	//Byte 20 - 23	offset
	
	if(p_cmd_head->Proces == 0)	//Get calib
	{
		channel = p_cmd_head->pDataBuf[0];
		mode = p_cmd_head->pDataBuf[2];
		params = p_cmd_head->pDataBuf[3];
        
		if(channel == 5)
		{
			mode = 5;
			params = 0;
			
			raw_low = iProfile.temperature.cold_calib[0].raw_lower;
			raw_high = iProfile.temperature.cold_calib[0].raw_higher;
			real_low = iProfile.temperature.cold_calib[0].real_lower;
			real_high = iProfile.temperature.cold_calib[0].real_higher;
			offset   = iProfile.temperature.cold_calib[0].offset;
			
			p_cmd_head->pDataBuf[0] = 5;	//channel
			p_cmd_head->pDataBuf[1] = 0;
			p_cmd_head->pDataBuf[2] = 5;	//mode
			p_cmd_head->pDataBuf[3] = 0;	//params
			
			p_cmd_head->pDataBuf[ 4 + 0] = ((raw_low >> 0)&0xFF);
			p_cmd_head->pDataBuf[ 4 + 1] = ((raw_low >> 8)&0xFF);
			p_cmd_head->pDataBuf[ 4 + 2] = ((raw_low >>16)&0xFF);
			p_cmd_head->pDataBuf[ 4 + 3] = ((raw_low >>24)&0xFF);
			
			p_cmd_head->pDataBuf[ 8 + 0] = ((raw_high>> 0)&0xFF);
			p_cmd_head->pDataBuf[ 8 + 1] = ((raw_high>> 8)&0xFF);
			p_cmd_head->pDataBuf[ 8 + 2] = ((raw_high>>16)&0xFF);
			p_cmd_head->pDataBuf[ 8 + 3] = ((raw_high>>24)&0xFF);
			
			p_cmd_head->pDataBuf[12 + 0] = ((real_low>> 0)&0xFF);
			p_cmd_head->pDataBuf[12 + 1] = ((real_low>> 8)&0xFF);
			p_cmd_head->pDataBuf[12 + 2] = ((real_low>>16)&0xFF);
			p_cmd_head->pDataBuf[12 + 3] = ((real_low>>24)&0xFF);
			
			p_cmd_head->pDataBuf[16 + 0] = ((real_high>> 0)&0xFF);
			p_cmd_head->pDataBuf[16 + 1] = ((real_high>> 8)&0xFF);
			p_cmd_head->pDataBuf[16 + 2] = ((real_high>>16)&0xFF);
			p_cmd_head->pDataBuf[16 + 3] = ((real_high>>24)&0xFF);
			
			p_cmd_head->pDataBuf[20 + 0] = ((offset>> 0)&0xFF);
			p_cmd_head->pDataBuf[20 + 1] = ((offset>> 8)&0xFF);
			p_cmd_head->pDataBuf[20 + 2] = ((offset>>16)&0xFF);
			p_cmd_head->pDataBuf[20 + 3] = ((offset>>24)&0xFF);
			
			CMD_SET_OK(p_cmd_head,36);
			goto end;
		}
		else if((channel >= 10)&&(channel < 127))
		{
			mode = 5;
			params = 0;
			
			temp_16 = channel - 10;
			
			if(temp_16 >= TEMPERATURE_MAX_CHANNEL)
			{
				temp_16 = TEMPERATURE_MAX_CHANNEL - 1;
			}
			
			raw_low = iProfile.temperature.calib[temp_16].raw_lower;
			raw_high = iProfile.temperature.calib[temp_16].raw_higher;
			real_low = iProfile.temperature.calib[temp_16].real_lower;
			real_high = iProfile.temperature.calib[temp_16].real_higher;
			offset   = iProfile.temperature.calib[temp_16].offset;
			
			p_cmd_head->pDataBuf[0] = channel;	//channel
			p_cmd_head->pDataBuf[1] = 0;
			p_cmd_head->pDataBuf[2] = 5;	//mode
			p_cmd_head->pDataBuf[3] = 0;	//params
			
			p_cmd_head->pDataBuf[ 4 + 0] = ((raw_low >> 0)&0xFF);
			p_cmd_head->pDataBuf[ 4 + 1] = ((raw_low >> 8)&0xFF);
			p_cmd_head->pDataBuf[ 4 + 2] = ((raw_low >>16)&0xFF);
			p_cmd_head->pDataBuf[ 4 + 3] = ((raw_low >>24)&0xFF);
			
			p_cmd_head->pDataBuf[ 8 + 0] = ((raw_high>> 0)&0xFF);
			p_cmd_head->pDataBuf[ 8 + 1] = ((raw_high>> 8)&0xFF);
			p_cmd_head->pDataBuf[ 8 + 2] = ((raw_high>>16)&0xFF);
			p_cmd_head->pDataBuf[ 8 + 3] = ((raw_high>>24)&0xFF);
			
			p_cmd_head->pDataBuf[12 + 0] = ((real_low>> 0)&0xFF);
			p_cmd_head->pDataBuf[12 + 1] = ((real_low>> 8)&0xFF);
			p_cmd_head->pDataBuf[12 + 2] = ((real_low>>16)&0xFF);
			p_cmd_head->pDataBuf[12 + 3] = ((real_low>>24)&0xFF);
			
			p_cmd_head->pDataBuf[16 + 0] = ((real_high>> 0)&0xFF);
			p_cmd_head->pDataBuf[16 + 1] = ((real_high>> 8)&0xFF);
			p_cmd_head->pDataBuf[16 + 2] = ((real_high>>16)&0xFF);
			p_cmd_head->pDataBuf[16 + 3] = ((real_high>>24)&0xFF);
			
			p_cmd_head->pDataBuf[20 + 0] = ((offset>> 0)&0xFF);
			p_cmd_head->pDataBuf[20 + 1] = ((offset>> 8)&0xFF);
			p_cmd_head->pDataBuf[20 + 2] = ((offset>>16)&0xFF);
			p_cmd_head->pDataBuf[20 + 3] = ((offset>>24)&0xFF);
			
			CMD_SET_OK(p_cmd_head,36);
			goto end;
		}
		else if((channel == 130)||(channel == 131))
		{
			mode = 5;
			params = 0;
			
			if(channel == 130)
			{
				temp_16 = 0;
			}
			else
			{
				temp_16 = 1;
			}
			
			raw_low = iProfile.pressure.calib[temp_16].raw_lower;
			raw_high = iProfile.pressure.calib[temp_16].raw_higher;
			real_low = iProfile.pressure.calib[temp_16].real_lower;
			real_high = iProfile.pressure.calib[temp_16].real_higher;
			offset   = iProfile.pressure.calib[temp_16].offset;
			
			p_cmd_head->pDataBuf[0] = channel;	//channel
			p_cmd_head->pDataBuf[1] = 0;
			p_cmd_head->pDataBuf[2] = 5;	//mode
			p_cmd_head->pDataBuf[3] = 0;	//params
			
			p_cmd_head->pDataBuf[ 4 + 0] = ((raw_low >> 0)&0xFF);
			p_cmd_head->pDataBuf[ 4 + 1] = ((raw_low >> 8)&0xFF);
			p_cmd_head->pDataBuf[ 4 + 2] = ((raw_low >>16)&0xFF);
			p_cmd_head->pDataBuf[ 4 + 3] = ((raw_low >>24)&0xFF);
			
			p_cmd_head->pDataBuf[ 8 + 0] = ((raw_high>> 0)&0xFF);
			p_cmd_head->pDataBuf[ 8 + 1] = ((raw_high>> 8)&0xFF);
			p_cmd_head->pDataBuf[ 8 + 2] = ((raw_high>>16)&0xFF);
			p_cmd_head->pDataBuf[ 8 + 3] = ((raw_high>>24)&0xFF);
			
			p_cmd_head->pDataBuf[12 + 0] = ((real_low>> 0)&0xFF);
			p_cmd_head->pDataBuf[12 + 1] = ((real_low>> 8)&0xFF);
			p_cmd_head->pDataBuf[12 + 2] = ((real_low>>16)&0xFF);
			p_cmd_head->pDataBuf[12 + 3] = ((real_low>>24)&0xFF);
			
			p_cmd_head->pDataBuf[16 + 0] = ((real_high>> 0)&0xFF);
			p_cmd_head->pDataBuf[16 + 1] = ((real_high>> 8)&0xFF);
			p_cmd_head->pDataBuf[16 + 2] = ((real_high>>16)&0xFF);
			p_cmd_head->pDataBuf[16 + 3] = ((real_high>>24)&0xFF);
			
			p_cmd_head->pDataBuf[20 + 0] = ((offset>> 0)&0xFF);
			p_cmd_head->pDataBuf[20 + 1] = ((offset>> 8)&0xFF);
			p_cmd_head->pDataBuf[20 + 2] = ((offset>>16)&0xFF);
			p_cmd_head->pDataBuf[20 + 3] = ((offset>>24)&0xFF);
			
			CMD_SET_OK(p_cmd_head,36);
			goto end;
		}
		else if((channel == 135)||(channel == 136))
		{
			mode = 5;
			
			if(channel == 135)
			{
				temp_16 = 0;
			}
			else
			{
				temp_16 = 1;
			}
			
			p_cmd_head->pDataBuf[0] = channel;	//channel
			p_cmd_head->pDataBuf[1] = 0;
			p_cmd_head->pDataBuf[2] = 15;	//mode multi2point
			p_cmd_head->pDataBuf[3] = 5;	//5point
            
            switch(params)
            {
                case 0:
                {
                    p_cmd_head->pDataBuf[ 4 + 0]= (iProfile.oxygen.mdp_calib.offset>>0)&0xFF;
                    p_cmd_head->pDataBuf[ 4 + 1]= (iProfile.oxygen.mdp_calib.offset>>8)&0xFF;
                    p_cmd_head->pDataBuf[ 4 + 2]= (iProfile.oxygen.mdp_calib.offset>>16)&0xFF;
                    p_cmd_head->pDataBuf[ 4 + 3]= (iProfile.oxygen.mdp_calib.offset>>24)&0xFF;			
                }break;
                
                case 1:
                {
                    p_cmd_head->pDataBuf[ 4 + 0]= (iProfile.oxygen.mdp_calib.p_x[0]>>0)&0xFF;
                    p_cmd_head->pDataBuf[ 4 + 1]= (iProfile.oxygen.mdp_calib.p_x[0]>>8)&0xFF;
                    p_cmd_head->pDataBuf[ 4 + 2]= (iProfile.oxygen.mdp_calib.p_x[0]>>16)&0xFF;
                    p_cmd_head->pDataBuf[ 4 + 3]= (iProfile.oxygen.mdp_calib.p_x[0]>>24)&0xFF;
                    
                    p_cmd_head->pDataBuf[ 8 + 0]=  (iProfile.oxygen.mdp_calib.p_y[0]>>0)&0xFF;
                    p_cmd_head->pDataBuf[ 8 + 1]=  (iProfile.oxygen.mdp_calib.p_y[0]>>8)&0xFF; 
                    p_cmd_head->pDataBuf[ 8 + 2] = (iProfile.oxygen.mdp_calib.p_y[0]>>16)&0xFF;
                    p_cmd_head->pDataBuf[ 8 + 3] = (iProfile.oxygen.mdp_calib.p_y[0]>>24)&0xFF; 
                }break;
                
                case 2:
                {
                    p_cmd_head->pDataBuf[ 4 + 0]= (iProfile.oxygen.mdp_calib.p_x[1]>>0)&0xFF;
                    p_cmd_head->pDataBuf[ 4 + 1]= (iProfile.oxygen.mdp_calib.p_x[1]>>8)&0xFF;
                    p_cmd_head->pDataBuf[ 4 + 2]= (iProfile.oxygen.mdp_calib.p_x[1]>>16)&0xFF;
                    p_cmd_head->pDataBuf[ 4 + 3]= (iProfile.oxygen.mdp_calib.p_x[1]>>24)&0xFF;
                    
                    p_cmd_head->pDataBuf[ 8 + 0]=  (iProfile.oxygen.mdp_calib.p_y[1]>>0)&0xFF;
                    p_cmd_head->pDataBuf[ 8 + 1]=  (iProfile.oxygen.mdp_calib.p_y[1]>>8)&0xFF; 
                    p_cmd_head->pDataBuf[ 8 + 2] = (iProfile.oxygen.mdp_calib.p_y[1]>>16)&0xFF;
                    p_cmd_head->pDataBuf[ 8 + 3] = (iProfile.oxygen.mdp_calib.p_y[1]>>24)&0xFF; 
                }break;
                
                case 3:
                {
                    p_cmd_head->pDataBuf[ 4 + 0]= (iProfile.oxygen.mdp_calib.p_x[2]>>0)&0xFF;
                    p_cmd_head->pDataBuf[ 4 + 1]= (iProfile.oxygen.mdp_calib.p_x[2]>>8)&0xFF;
                    p_cmd_head->pDataBuf[ 4 + 2]= (iProfile.oxygen.mdp_calib.p_x[2]>>16)&0xFF;
                    p_cmd_head->pDataBuf[ 4 + 3]= (iProfile.oxygen.mdp_calib.p_x[2]>>24)&0xFF;
                    
                    p_cmd_head->pDataBuf[ 8 + 0]=  (iProfile.oxygen.mdp_calib.p_y[2]>>0)&0xFF;
                    p_cmd_head->pDataBuf[ 8 + 1]=  (iProfile.oxygen.mdp_calib.p_y[2]>>8)&0xFF; 
                    p_cmd_head->pDataBuf[ 8 + 2] = (iProfile.oxygen.mdp_calib.p_y[2]>>16)&0xFF;
                    p_cmd_head->pDataBuf[ 8 + 3] = (iProfile.oxygen.mdp_calib.p_y[2]>>24)&0xFF; 
                }break;
                
                case 4:
                {
                    p_cmd_head->pDataBuf[ 4 + 0]= (iProfile.oxygen.mdp_calib.p_x[3]>>0)&0xFF;
                    p_cmd_head->pDataBuf[ 4 + 1]= (iProfile.oxygen.mdp_calib.p_x[3]>>8)&0xFF;
                    p_cmd_head->pDataBuf[ 4 + 2]= (iProfile.oxygen.mdp_calib.p_x[3]>>16)&0xFF;
                    p_cmd_head->pDataBuf[ 4 + 3]= (iProfile.oxygen.mdp_calib.p_x[3]>>24)&0xFF;
                    
                    p_cmd_head->pDataBuf[ 8 + 0]=  (iProfile.oxygen.mdp_calib.p_y[3]>>0)&0xFF;
                    p_cmd_head->pDataBuf[ 8 + 1]=  (iProfile.oxygen.mdp_calib.p_y[3]>>8)&0xFF; 
                    p_cmd_head->pDataBuf[ 8 + 2] = (iProfile.oxygen.mdp_calib.p_y[3]>>16)&0xFF;
                    p_cmd_head->pDataBuf[ 8 + 3] = (iProfile.oxygen.mdp_calib.p_y[3]>>24)&0xFF; 
                }break;
                
                case 5:
                {
                    p_cmd_head->pDataBuf[ 4 + 0]= (iProfile.oxygen.mdp_calib.p_x[4]>>0)&0xFF;
                    p_cmd_head->pDataBuf[ 4 + 1]= (iProfile.oxygen.mdp_calib.p_x[4]>>8)&0xFF;
                    p_cmd_head->pDataBuf[ 4 + 2]= (iProfile.oxygen.mdp_calib.p_x[4]>>16)&0xFF;
                    p_cmd_head->pDataBuf[ 4 + 3]= (iProfile.oxygen.mdp_calib.p_x[4]>>24)&0xFF;
                    
                    p_cmd_head->pDataBuf[ 8 + 0]=  (iProfile.oxygen.mdp_calib.p_y[4]>>0)&0xFF;
                    p_cmd_head->pDataBuf[ 8 + 1]=  (iProfile.oxygen.mdp_calib.p_y[4]>>8)&0xFF; 
                    p_cmd_head->pDataBuf[ 8 + 2] = (iProfile.oxygen.mdp_calib.p_y[4]>>16)&0xFF;
                    p_cmd_head->pDataBuf[ 8 + 3] = (iProfile.oxygen.mdp_calib.p_y[4]>>24)&0xFF; 
                }break;
            }
                    			
			
			CMD_SET_OK(p_cmd_head,12);
			goto end;
		}
		else if((channel == 140)||(channel == 141))
		{
			
		}
		else if((channel == 145)||(channel == 146)) //Get Angle pitch roll calib
		{
			mode = 5;
			params = 0;
			
			if(channel == 145)
			{
				temp_16 = 0;
			}
			else
			{
				temp_16 = 1;
			}
			
			raw_low = iProfile.vibrate.calib[temp_16].raw_lower;
			raw_high = iProfile.vibrate.calib[temp_16].raw_higher;
			real_low = iProfile.vibrate.calib[temp_16].real_lower;
			real_high = iProfile.vibrate.calib[temp_16].real_higher;
			offset   = iProfile.vibrate.calib[temp_16].offset;
			
			p_cmd_head->pDataBuf[0] = channel;	//channel
			p_cmd_head->pDataBuf[1] = 0;
			p_cmd_head->pDataBuf[2] = 5;	//mode
			p_cmd_head->pDataBuf[3] = 0;	//params
			
			p_cmd_head->pDataBuf[ 4 + 0] = ((raw_low >> 0)&0xFF);
			p_cmd_head->pDataBuf[ 4 + 1] = ((raw_low >> 8)&0xFF);
			p_cmd_head->pDataBuf[ 4 + 2] = ((raw_low >>16)&0xFF);
			p_cmd_head->pDataBuf[ 4 + 3] = ((raw_low >>24)&0xFF);
			
			p_cmd_head->pDataBuf[ 8 + 0] = ((raw_high>> 0)&0xFF);
			p_cmd_head->pDataBuf[ 8 + 1] = ((raw_high>> 8)&0xFF);
			p_cmd_head->pDataBuf[ 8 + 2] = ((raw_high>>16)&0xFF);
			p_cmd_head->pDataBuf[ 8 + 3] = ((raw_high>>24)&0xFF);
			
			p_cmd_head->pDataBuf[12 + 0] = ((real_low>> 0)&0xFF);
			p_cmd_head->pDataBuf[12 + 1] = ((real_low>> 8)&0xFF);
			p_cmd_head->pDataBuf[12 + 2] = ((real_low>>16)&0xFF);
			p_cmd_head->pDataBuf[12 + 3] = ((real_low>>24)&0xFF);
			
			p_cmd_head->pDataBuf[16 + 0] = ((real_high>> 0)&0xFF);
			p_cmd_head->pDataBuf[16 + 1] = ((real_high>> 8)&0xFF);
			p_cmd_head->pDataBuf[16 + 2] = ((real_high>>16)&0xFF);
			p_cmd_head->pDataBuf[16 + 3] = ((real_high>>24)&0xFF);
			
			p_cmd_head->pDataBuf[20 + 0] = ((offset>> 0)&0xFF);
			p_cmd_head->pDataBuf[20 + 1] = ((offset>> 8)&0xFF);
			p_cmd_head->pDataBuf[20 + 2] = ((offset>>16)&0xFF);
			p_cmd_head->pDataBuf[20 + 3] = ((offset>>24)&0xFF);
			
			CMD_SET_OK(p_cmd_head,36);
			goto end;
		}
        else if((channel == 150)||(channel == 151)||(channel == 152)) //Get Angle pitch roll calib
		{
			mode = 5;
			params = 0;
			
			if(channel == 150)
			{
				temp_16 = 2;
			}
			else if(channel == 151)
			{
				temp_16 = 3;
			}
            else if(channel == 152)
			{
				temp_16 = 4;
			}
			
			raw_low = iProfile.vibrate.calib[temp_16].raw_lower;
			raw_high = iProfile.vibrate.calib[temp_16].raw_higher;
			real_low = iProfile.vibrate.calib[temp_16].real_lower;
			real_high = iProfile.vibrate.calib[temp_16].real_higher;
			offset   = iProfile.vibrate.calib[temp_16].offset;
			
			p_cmd_head->pDataBuf[0] = channel;	//channel
			p_cmd_head->pDataBuf[1] = 0;
			p_cmd_head->pDataBuf[2] = 5;	//mode
			p_cmd_head->pDataBuf[3] = 0;	//params
			
			p_cmd_head->pDataBuf[ 4 + 0] = ((raw_low >> 0)&0xFF);
			p_cmd_head->pDataBuf[ 4 + 1] = ((raw_low >> 8)&0xFF);
			p_cmd_head->pDataBuf[ 4 + 2] = ((raw_low >>16)&0xFF);
			p_cmd_head->pDataBuf[ 4 + 3] = ((raw_low >>24)&0xFF);
			
			p_cmd_head->pDataBuf[ 8 + 0] = ((raw_high>> 0)&0xFF);
			p_cmd_head->pDataBuf[ 8 + 1] = ((raw_high>> 8)&0xFF);
			p_cmd_head->pDataBuf[ 8 + 2] = ((raw_high>>16)&0xFF);
			p_cmd_head->pDataBuf[ 8 + 3] = ((raw_high>>24)&0xFF);
			
			p_cmd_head->pDataBuf[12 + 0] = ((real_low>> 0)&0xFF);
			p_cmd_head->pDataBuf[12 + 1] = ((real_low>> 8)&0xFF);
			p_cmd_head->pDataBuf[12 + 2] = ((real_low>>16)&0xFF);
			p_cmd_head->pDataBuf[12 + 3] = ((real_low>>24)&0xFF);
			
			p_cmd_head->pDataBuf[16 + 0] = ((real_high>> 0)&0xFF);
			p_cmd_head->pDataBuf[16 + 1] = ((real_high>> 8)&0xFF);
			p_cmd_head->pDataBuf[16 + 2] = ((real_high>>16)&0xFF);
			p_cmd_head->pDataBuf[16 + 3] = ((real_high>>24)&0xFF);
			
			p_cmd_head->pDataBuf[20 + 0] = ((offset>> 0)&0xFF);
			p_cmd_head->pDataBuf[20 + 1] = ((offset>> 8)&0xFF);
			p_cmd_head->pDataBuf[20 + 2] = ((offset>>16)&0xFF);
			p_cmd_head->pDataBuf[20 + 3] = ((offset>>24)&0xFF);
			
			CMD_SET_OK(p_cmd_head,36);
			goto end;
		}
	}
	else if(p_cmd_head->Proces == 1)//Set calib
	{
		channel = p_cmd_head->pDataBuf[0];
		mode = p_cmd_head->pDataBuf[2];
		params = p_cmd_head->pDataBuf[3];
		
		raw_low =  (p_cmd_head->pDataBuf[ 4 + 0]<<0) | (p_cmd_head->pDataBuf[ 4 + 1]<<8) | (p_cmd_head->pDataBuf[ 4 + 2]<<16) | (p_cmd_head->pDataBuf[ 4 + 3]<<24);
		raw_high = (p_cmd_head->pDataBuf[ 8 + 0]<<0) | (p_cmd_head->pDataBuf[ 8 + 1]<<8) | (p_cmd_head->pDataBuf[ 8 + 2]<<16) | (p_cmd_head->pDataBuf[ 8 + 3]<<24);
		real_low = (p_cmd_head->pDataBuf[12 + 0]<<0) | (p_cmd_head->pDataBuf[12 + 1]<<8) | (p_cmd_head->pDataBuf[12 + 2]<<16) | (p_cmd_head->pDataBuf[12 + 3]<<24);
		real_high =(p_cmd_head->pDataBuf[16 + 0]<<0) | (p_cmd_head->pDataBuf[16 + 1]<<8) | (p_cmd_head->pDataBuf[16 + 2]<<16) | (p_cmd_head->pDataBuf[16 + 3]<<24);
		offset 	  =(p_cmd_head->pDataBuf[20 + 0]<<0) | (p_cmd_head->pDataBuf[20 + 1]<<8) | (p_cmd_head->pDataBuf[20 + 2]<<16) | (p_cmd_head->pDataBuf[20 + 3]<<24);
		
		
		if(channel == 5)
		{
			if(mode != 5)
			{
				CMD_SET_ERROR(p_cmd_head,CMD_INVPARAMS);	
				goto end;
			}
				
			iProfile.temperature.cold_calib[0].raw_lower = raw_low;
			iProfile.temperature.cold_calib[0].raw_higher = raw_high;
			iProfile.temperature.cold_calib[0].real_lower = real_low;
			iProfile.temperature.cold_calib[0].real_higher = real_high;
			iProfile.temperature.cold_calib[0].offset = offset;
			
			calib_dpoint_cal(&iProfile.temperature.cold_calib[0]);
			
			CMD_SET_OK(p_cmd_head,0);
			goto end;
		}
		else if((channel >= 10)&&(channel < 127))
		{
			if(mode != 5)
			{
				CMD_SET_ERROR(p_cmd_head,CMD_INVPARAMS);	
				goto end;
			}
			
			temp_16 = channel - 10;
			
			if(temp_16 >= TEMPERATURE_MAX_CHANNEL)
			{
				temp_16 = TEMPERATURE_MAX_CHANNEL - 1;
			}
			
			iProfile.temperature.calib[temp_16].raw_lower = raw_low;
			iProfile.temperature.calib[temp_16].raw_higher = raw_high;
			iProfile.temperature.calib[temp_16].real_lower = real_low;
			iProfile.temperature.calib[temp_16].real_higher = real_high;
			iProfile.temperature.calib[temp_16].offset = offset;
			
			calib_dpoint_cal(&iProfile.temperature.calib[temp_16]);
			
			CMD_SET_OK(p_cmd_head,0);
			goto end;
		}
		else if((channel == 130)||(channel == 131))
		{
			if(mode != 5)
			{
				CMD_SET_ERROR(p_cmd_head,CMD_INVPARAMS);	
				goto end;
			}
			
			if(channel == 130)
			{
				temp_16 = 0;
			}
			else
			{
				temp_16 = 1;
			}
			
			iProfile.pressure.calib[temp_16].raw_lower = raw_low;
			iProfile.pressure.calib[temp_16].raw_higher = raw_high;
			iProfile.pressure.calib[temp_16].real_lower = real_low;
			iProfile.pressure.calib[temp_16].real_higher = real_high;
			iProfile.pressure.calib[temp_16].offset = offset;
			
			calib_dpoint_cal(&iProfile.pressure.calib[temp_16]);
			
			CMD_SET_OK(p_cmd_head,0);
			goto end;
		}
		else if((channel == 135)||(channel == 136))
		{
			if(mode != 15)
			{
				CMD_SET_ERROR(p_cmd_head,CMD_INVPARAMS);	
				goto end;
			}
			
			if(channel == 135)
			{
				temp_16 = 0;
			}
			else
			{
				temp_16 = 1;
			}
			
            switch(params)
            {
                case 0:iProfile.oxygen.mdp_calib.offset = (p_cmd_head->pDataBuf[ 4 + 0]<<0) | (p_cmd_head->pDataBuf[ 4 + 1]<<8) | (p_cmd_head->pDataBuf[ 4 + 2]<<16) | (p_cmd_head->pDataBuf[ 4 + 3]<<24);break;
                case 1:
                {
                    iProfile.oxygen.mdp_calib.p_x[0] = (p_cmd_head->pDataBuf[ 4 + 0]<<0) | (p_cmd_head->pDataBuf[ 4 + 1]<<8) | (p_cmd_head->pDataBuf[ 4 + 2]<<16) | (p_cmd_head->pDataBuf[ 4 + 3]<<24);
                    iProfile.oxygen.mdp_calib.p_y[0] = (p_cmd_head->pDataBuf[ 8 + 0]<<0) | (p_cmd_head->pDataBuf[ 8 + 1]<<8) | (p_cmd_head->pDataBuf[ 8 + 2]<<16) | (p_cmd_head->pDataBuf[ 8 + 3]<<24);
                }break;
                
                case 2:
                {
                    iProfile.oxygen.mdp_calib.p_x[1] = (p_cmd_head->pDataBuf[ 4 + 0]<<0) | (p_cmd_head->pDataBuf[ 4 + 1]<<8) | (p_cmd_head->pDataBuf[ 4 + 2]<<16) | (p_cmd_head->pDataBuf[ 4 + 3]<<24);
                    iProfile.oxygen.mdp_calib.p_y[1] = (p_cmd_head->pDataBuf[ 8 + 0]<<0) | (p_cmd_head->pDataBuf[ 8 + 1]<<8) | (p_cmd_head->pDataBuf[ 8 + 2]<<16) | (p_cmd_head->pDataBuf[ 8 + 3]<<24);
                }break;
                
                case 3:
                {
                    iProfile.oxygen.mdp_calib.p_x[2] = (p_cmd_head->pDataBuf[ 4 + 0]<<0) | (p_cmd_head->pDataBuf[ 4 + 1]<<8) | (p_cmd_head->pDataBuf[ 4 + 2]<<16) | (p_cmd_head->pDataBuf[ 4 + 3]<<24);
                    iProfile.oxygen.mdp_calib.p_y[2] = (p_cmd_head->pDataBuf[ 8 + 0]<<0) | (p_cmd_head->pDataBuf[ 8 + 1]<<8) | (p_cmd_head->pDataBuf[ 8 + 2]<<16) | (p_cmd_head->pDataBuf[ 8 + 3]<<24);
                }break;
                
                case 4:
                {
                    iProfile.oxygen.mdp_calib.p_x[3] = (p_cmd_head->pDataBuf[ 4 + 0]<<0) | (p_cmd_head->pDataBuf[ 4 + 1]<<8) | (p_cmd_head->pDataBuf[ 4 + 2]<<16) | (p_cmd_head->pDataBuf[ 4 + 3]<<24);
                    iProfile.oxygen.mdp_calib.p_y[3] = (p_cmd_head->pDataBuf[ 8 + 0]<<0) | (p_cmd_head->pDataBuf[ 8 + 1]<<8) | (p_cmd_head->pDataBuf[ 8 + 2]<<16) | (p_cmd_head->pDataBuf[ 8 + 3]<<24);
                }break;
                
                case 5:
                {
                    iProfile.oxygen.mdp_calib.p_x[4] = (p_cmd_head->pDataBuf[ 4 + 0]<<0) | (p_cmd_head->pDataBuf[ 4 + 1]<<8) | (p_cmd_head->pDataBuf[ 4 + 2]<<16) | (p_cmd_head->pDataBuf[ 4 + 3]<<24);
                    iProfile.oxygen.mdp_calib.p_y[4] = (p_cmd_head->pDataBuf[ 8 + 0]<<0) | (p_cmd_head->pDataBuf[ 8 + 1]<<8) | (p_cmd_head->pDataBuf[ 8 + 2]<<16) | (p_cmd_head->pDataBuf[ 8 + 3]<<24);
                }break;
            }
           	iProfile.oxygen.mdp_calib.calib_point = 5;				
			calib_mdpoint_cal(&iProfile.oxygen.mdp_calib);
			
			CMD_SET_OK(p_cmd_head,0);
			goto end;
		}
		else if((channel == 140)||(channel == 141))
		{
			
		}
		else if((channel == 145)||(channel == 146)) //Set Angle pitch roll calib
		{
			if(mode != 5)
			{
				CMD_SET_ERROR(p_cmd_head,CMD_INVPARAMS);	
				goto end;
			}
			
			if(channel == 145)
			{
				temp_16 = 0;
			}
			else
			{
				temp_16 = 1;
			}
			
			iProfile.vibrate.calib[temp_16].raw_lower = raw_low;
			iProfile.vibrate.calib[temp_16].raw_higher = raw_high;
			iProfile.vibrate.calib[temp_16].real_lower = real_low;
			iProfile.vibrate.calib[temp_16].real_higher = real_high;
			iProfile.vibrate.calib[temp_16].offset = offset;
			
			calib_dpoint_cal(&iProfile.vibrate.calib[temp_16]);
			
			CMD_SET_OK(p_cmd_head,0);
			goto end;
		}
        else if((channel == 150)||(channel == 151)||(channel == 152)) //Set vibration calib
		{
			if(mode != 5)
			{
				CMD_SET_ERROR(p_cmd_head,CMD_INVPARAMS);	
				goto end;
			}
			
			if(channel == 150)
			{
				temp_16 = 2;
			}
			else if(channel == 151)
			{
				temp_16 = 3;
			}
            else if(channel == 152)
			{
				temp_16 = 4;
			}
			
			iProfile.vibrate.calib[temp_16].raw_lower = raw_low;
			iProfile.vibrate.calib[temp_16].raw_higher = raw_high;
			iProfile.vibrate.calib[temp_16].real_lower = real_low;
			iProfile.vibrate.calib[temp_16].real_higher = real_high;
			iProfile.vibrate.calib[temp_16].offset = offset;
			
			calib_dpoint_cal(&iProfile.vibrate.calib[temp_16]);
			
			CMD_SET_OK(p_cmd_head,0);
			goto end;
		}
	}
	else if(p_cmd_head->Proces == 2)//save calib
	{
		channel = p_cmd_head->pDataBuf[0];
		
		if(channel == 5)
		{
			nvm_thermo_calib_save();
			CMD_SET_OK(p_cmd_head,0);
			goto end;
		}
		else if((channel >= 10)&&(channel < 127))
		{
			
		}
		else if((channel == 130)||(channel == 131))
		{
			nvm_calib_pressure_save();
			CMD_SET_OK(p_cmd_head,0);
			goto end;
		}
		else if((channel == 135)||(channel == 136))
		{
			nvm_calib_oxygen_save();
			CMD_SET_OK(p_cmd_head,0);
			goto end;
		}
		else if((channel == 140)||(channel == 141))
		{
			
		}
		else if((channel == 145)||(channel == 146))
		{
			nvm_calib_angle_save();
			CMD_SET_OK(p_cmd_head,0);
			goto end;
		}
        else if(channel == 150)
		{
			nvm_calib_angle_save();
			CMD_SET_OK(p_cmd_head,0);
			goto end;
		}
	}
	else
	{
		CMD_SET_ERROR(p_cmd_head,CMD_INVPARAMS);
		goto end;
	}
	
end:
	return 0;
}
//


/*****************************************************************************************
* Function Name  : cb_get_dev_info
* Description    : 
* Input          : None
* Output         : None
* Return         : None
*****************************************************************************************/
int32_t cb_get_dev_conf(frame_head_t *p_cmd_head)
{
	uint8_t i;

	switch(p_cmd_head->Proces)
	{
		case 0:
		{					
			p_cmd_head->pDataBuf[0] = iProfile.conf.dev_main_type;
			p_cmd_head->pDataBuf[1] = iProfile.conf.dev_sub_type;
			p_cmd_head->pDataBuf[2] = iProfile.conf.dev_ext_type;
			p_cmd_head->pDataBuf[3] = iProfile.conf.check_id;
			p_cmd_head->pDataBuf[4] = iProfile.conf.version_Major;
			p_cmd_head->pDataBuf[5] = iProfile.conf.version_minor;
			p_cmd_head->pDataBuf[6] = iProfile.conf.version_revison;
			p_cmd_head->pDataBuf[7] = iProfile.conf.version_label;

			p_cmd_head->pDataBuf[8] = iProfile.conf.mfg_year;
			p_cmd_head->pDataBuf[9] = iProfile.conf.mfg_month;
			p_cmd_head->pDataBuf[10] = iProfile.conf.mfg_day;
			p_cmd_head->pDataBuf[11] = iProfile.conf.Total_channel;
			p_cmd_head->pDataBuf[12] = iProfile.conf.dummy_1;
			p_cmd_head->pDataBuf[13] = iProfile.conf.dummy_2;

			for(i=0;i<36;i++)
			{
				p_cmd_head->pDataBuf[i+14] = iProfile.conf.serial_id[i];
			}
	
			p_cmd_head->DataLength =50;
			p_cmd_head->DeviceStatus = CMD_SUCCESS;
		}break;

		case 1:
		{
			p_cmd_head->pDataBuf[0] = iProfile.conf.calib_year;
			p_cmd_head->pDataBuf[1] = iProfile.conf.calib_month;
			p_cmd_head->pDataBuf[2] = iProfile.conf.calib_day;
			p_cmd_head->pDataBuf[3] = iProfile.conf.calib_next_year;
			p_cmd_head->pDataBuf[4] = iProfile.conf.calib_next_month;
			p_cmd_head->pDataBuf[5] = iProfile.conf.calib_next_day;
			p_cmd_head->pDataBuf[6] = iProfile.conf.warranty_year;
			p_cmd_head->pDataBuf[7] = iProfile.conf.warranty_month;
			p_cmd_head->pDataBuf[8] = iProfile.conf.warranty_day;
			p_cmd_head->pDataBuf[9] = MAX_RECODER_GROUP_CNT;
			
			p_cmd_head->pDataBuf[10] = (iProfile.conf.over_temp_stop>>0)&0xFF;
			p_cmd_head->pDataBuf[11] = (iProfile.conf.over_temp_stop>>8)&0xFF;
			
			p_cmd_head->pDataBuf[12] = (iProfile.conf.start_max_temp>>0)&0xFF;
			p_cmd_head->pDataBuf[13] = (iProfile.conf.start_max_temp>>8)&0xFF;
			
			p_cmd_head->pDataBuf[14] = (iProfile.conf.start_max_tc_temp>>0)&0xFF;
			p_cmd_head->pDataBuf[15] = (iProfile.conf.start_max_tc_temp>>8)&0xFF;

			for(i=0;i<32;i++)
			{
				p_cmd_head->pDataBuf[i + 16] = iProfile.conf.dev_name[i];
			}
			
			p_cmd_head->pDataBuf[48] = (iProfile.conf.Bat_capacity>>0)&0xFF;
			p_cmd_head->pDataBuf[49] = (iProfile.conf.Bat_capacity>>8)&0xFF;
			
			p_cmd_head->DataLength =50;
			p_cmd_head->DeviceStatus = CMD_SUCCESS;

		}break;

		default: {
			p_cmd_head->DataLength =0;
			p_cmd_head->DeviceStatus = CMD_INVPARAMS;
		}break;
	}

	return 0;
}
//


/*****************************************************************************************
* Function Name  : cb_get_dev_info
* Description    : 
* Input          : None
* Output         : None
* Return         : None
*****************************************************************************************/
int32_t cb_set_dev_conf(frame_head_t *p_cmd_head)
{
	uint8_t i;
	
	switch(p_cmd_head->Proces)
	{		
		case 0:
		{	
			if(p_cmd_head->DataLength != 50)
			{
				p_cmd_head->DataLength =0;
				p_cmd_head->DeviceStatus = CMD_INVPARAMS;	
				break;
			}
			
			iProfile.conf.dev_main_type = DEVICE_TYPE;//params_buf[0]
			iProfile.conf.dev_sub_type = DEV_TYPE;//params_buf[1]
			iProfile.conf.dev_ext_type = p_cmd_head->pDataBuf[2];
			iProfile.conf.check_id = p_cmd_head->pDataBuf[3];
			iProfile.conf.version_Major = FW_VER_MAJOR;//params_buf[4]
			iProfile.conf.version_minor = 	FW_VER_MINOR;//params_buf[5]
			iProfile.conf.version_revison = FW_VER_REVISION;//params_buf[6]
			iProfile.conf.version_label = 	FW_VER_LABEL;//params_buf[7]


			iProfile.conf.mfg_year = p_cmd_head->pDataBuf[8];
			iProfile.conf.mfg_month = p_cmd_head->pDataBuf[9];
			iProfile.conf.mfg_day = p_cmd_head->pDataBuf[10];
			iProfile.conf.Total_channel = p_cmd_head->pDataBuf[11];
			iProfile.conf.dummy_1 = p_cmd_head->pDataBuf[12];
			iProfile.conf.dummy_2 = p_cmd_head->pDataBuf[13];

			for(i=0;i<36;i++)
			{
				iProfile.conf.serial_id[i] = p_cmd_head->pDataBuf[i + 14];
			}

			p_cmd_head->DataLength =0;
			p_cmd_head->DeviceStatus = CMD_SUCCESS;	
		}break;
		
		case 1:
		{
			if(p_cmd_head->DataLength != 50)
			{
				p_cmd_head->DataLength =0;
				p_cmd_head->DeviceStatus = CMD_INVPARAMS;	
				break;
			}
			
			iProfile.conf.calib_year = p_cmd_head->pDataBuf[0];
			iProfile.conf.calib_month = p_cmd_head->pDataBuf[1];
			iProfile.conf.calib_day = p_cmd_head->pDataBuf[2];
			iProfile.conf.calib_next_year = p_cmd_head->pDataBuf[3];
			iProfile.conf.calib_next_month = p_cmd_head->pDataBuf[4];
			iProfile.conf.calib_next_day = p_cmd_head->pDataBuf[5];
			iProfile.conf.warranty_year = p_cmd_head->pDataBuf[6];
			iProfile.conf.warranty_month = p_cmd_head->pDataBuf[7];
			iProfile.conf.warranty_day = p_cmd_head->pDataBuf[8];
			//iProfile.conf.dummy_3 = p_cmd_head->pDataBuf[9];
			
			iProfile.conf.over_temp_stop = (p_cmd_head->pDataBuf[10]<<0)|(p_cmd_head->pDataBuf[11]<<8);
			iProfile.conf.start_max_temp = (p_cmd_head->pDataBuf[12]<<0)|(p_cmd_head->pDataBuf[13]<<8);
			iProfile.conf.start_max_tc_temp = (p_cmd_head->pDataBuf[14]<<0)|(p_cmd_head->pDataBuf[15]<<8);
			
			for(i=0;i<32;i++)
			{
				iProfile.conf.dev_name[i] = p_cmd_head->pDataBuf[i + 16];
			}	
			iProfile.conf.Bat_capacity = (p_cmd_head->pDataBuf[48]<<0)|(p_cmd_head->pDataBuf[49]<<8);
			
			p_cmd_head->DataLength =0;
			p_cmd_head->DeviceStatus = CMD_SUCCESS;	
		}break;
		
		case 2:
		{
			if(	(p_cmd_head->pDataBuf[0] == 0xCC)&&
				(p_cmd_head->pDataBuf[1] == 0xAA)&&
				(p_cmd_head->pDataBuf[2] == 0x55)&&
				(p_cmd_head->pDataBuf[3] == 0xCC))
			{
				Mem_device_config_save();
				
				Mem_device_config_Load();
				
				p_cmd_head->DataLength =0;
				p_cmd_head->DeviceStatus = CMD_SUCCESS;
			}
			else
			{
				p_cmd_head->DataLength =0;
				p_cmd_head->DeviceStatus = CMD_INVPARAMS;
			}
		}break;
		
		default:
		{
			p_cmd_head->DataLength =0;
			p_cmd_head->DeviceStatus = CMD_INVPARAMS;
		}break;
	}
	return 0;
}

/*****************************************************************************************
* Function Name  : cb_get_dev_info
* Description    : 
* Input          : None
* Output         : None
* Return         : None
*****************************************************************************************/
int32_t cb_dev_conf_opera(frame_head_t *p_cmd_head)
{
	uint8_t i;
	uint8_t *pbuf;
	switch(p_cmd_head->Proces)
	{		
		case 0://get device conf
		{	
			pbuf = get_dev_mem();
			if(pbuf == 0)
			{
				for(i=0;i<48;i++)
				{
					p_cmd_head->pDataBuf[i] = 0;
				}
			}
			else
			{
				for(i=0;i<48;i++)
				{
					p_cmd_head->pDataBuf[i] = pbuf[i];
				}
			}
			
			p_cmd_head->DataLength = 52;
			p_cmd_head->DeviceStatus = CMD_SUCCESS;	
		}break;
		
		case 1://set device conf
		{
			set_dev_mem(&p_cmd_head->pDataBuf[4],48);
			
			p_cmd_head->DataLength =0;
			p_cmd_head->DeviceStatus = CMD_SUCCESS;	
		}break;
		
		
		default:
		{
			p_cmd_head->DataLength =0;
			p_cmd_head->DeviceStatus = CMD_INVPARAMS;
		}break;
	}
	return 0;
}

//
uint8_t calibre_timer_flag;
uint16_t calib_year,calib_month,calib_day,calib_hour,calib_minute,calib_second;
/*****************************************************************************************
* Function Name  : cb_SetDateTime
* Description    : 
* Input          : None
* Output         : None
* Return         : None
*****************************************************************************************/
int32_t cb_SetDateTime(frame_head_t *p_cmd_head)
{
	//uint16_t year,month,day,hour,minute,second;
	
	if(p_cmd_head->Proces != 0)
	{
		p_cmd_head->DataLength = 0;
		p_cmd_head->DeviceStatus = CMD_INVPARAMS;
		return 1;
	}
	
	calib_year = p_cmd_head->pDataBuf[0];
	calib_month = p_cmd_head->pDataBuf[1];
	calib_day = p_cmd_head->pDataBuf[2];
	
	calib_hour = p_cmd_head->pDataBuf[3];
	calib_minute = p_cmd_head->pDataBuf[4];
	calib_second = p_cmd_head->pDataBuf[5];
	

    xSemaphoreGive(sem_iprofile);		
    calibre_timer_flag = 1;

	//RTC_InitTime(calib_year,calib_month,calib_day,calib_hour,calib_minute,calib_second);

	p_cmd_head->DataLength = 0;
	p_cmd_head->DeviceStatus = CMD_SUCCESS;
	return 0;
}
//
/*****************************************************************************************
* Function Name  : cb_SetDateTime
* Description    : 
* Input          : None
* Output         : None
* Return         : None
*****************************************************************************************/
int32_t cb_GetMeasParameter(frame_head_t *p_cmd_head)
{
	
	if(p_cmd_head->Proces != 0)
	{
		p_cmd_head->DataLength = 0;
		p_cmd_head->DeviceStatus = CMD_INVPARAMS;
		return 1;
	}
	
	*(uint32_t*)(&p_cmd_head->pDataBuf[0]) =  iProfile.meas_params.SamplingSpan;
	*(uint32_t*)(&p_cmd_head->pDataBuf[4]) =  iProfile.meas_params.Start_Cond;
	*(uint32_t*)(&p_cmd_head->pDataBuf[8]) =  iProfile.meas_params.Start_Params_1;
	*(uint32_t*)(&p_cmd_head->pDataBuf[12]) = iProfile.meas_params.Start_Params_2;
	*(uint32_t*)(&p_cmd_head->pDataBuf[16]) = iProfile.meas_params.Start_Params_3;
	*(uint32_t*)(&p_cmd_head->pDataBuf[20]) = iProfile.meas_params.Start_Params_4;
	*(uint32_t*)(&p_cmd_head->pDataBuf[24]) = iProfile.meas_params.Stop_Cond;
	*(uint32_t*)(&p_cmd_head->pDataBuf[28]) = iProfile.meas_params.Stop_Params_1;
	*(uint32_t*)(&p_cmd_head->pDataBuf[32]) = iProfile.meas_params.Stop_Params_2;
	*(uint32_t*)(&p_cmd_head->pDataBuf[36]) = iProfile.meas_params.Stop_Params_3;
	
	p_cmd_head->DataLength = 40;
	p_cmd_head->DeviceStatus = CMD_SUCCESS;	
	
	return 0;
}
//

/*****************************************************************************************
* Function Name  : cb_SetDateTime
* Description    : 
* Input          : None
* Output         : None
* Return         : None
*****************************************************************************************/
int32_t cb_SetMeasParameter(frame_head_t *p_cmd_head)
{
	if(p_cmd_head->Proces != 0)
	{
		p_cmd_head->DataLength = 46;
		p_cmd_head->DeviceStatus = CMD_INVPARAMS;
		return 1;
	}
	
	if(p_cmd_head->DataLength != 40)
	{
		p_cmd_head->DataLength = 0;
		p_cmd_head->DeviceStatus = CMD_INVPARAMS;
		return 1;
	}
	
	iProfile.meas_params.SamplingSpan=			*(uint32_t*)(&p_cmd_head->pDataBuf[0]) ;
	iProfile.meas_params.Start_Cond=          *(uint32_t*)(&p_cmd_head->pDataBuf[4]) ;
	iProfile.meas_params.Start_Params_1=        *(uint32_t*)(&p_cmd_head->pDataBuf[8]) ;
	iProfile.meas_params.Start_Params_2=       *(uint32_t*)(&p_cmd_head->pDataBuf[12]);
	iProfile.meas_params.Start_Params_3=       *(uint32_t*)(&p_cmd_head->pDataBuf[16]);
	iProfile.meas_params.Start_Params_4=       *(uint32_t*)(&p_cmd_head->pDataBuf[20]);
	iProfile.meas_params.Stop_Cond=            *(uint32_t*)(&p_cmd_head->pDataBuf[24]);
	iProfile.meas_params.Stop_Params_1=       *(uint32_t*)(&p_cmd_head->pDataBuf[28]);
	iProfile.meas_params.Stop_Params_2=        *(uint32_t*)(&p_cmd_head->pDataBuf[32]);
	iProfile.meas_params.Stop_Params_3=        *(uint32_t*)(&p_cmd_head->pDataBuf[36]);
	
	Mem_MeasParams_Save();
	
	p_cmd_head->DataLength = 0;
	p_cmd_head->DeviceStatus = CMD_SUCCESS;	
	
	return 0;
}
//


/*****************************************************************************************
* Function Name  : cb_SetDateTime
* Description    : 
* Input          : None
* Output         : None
* Return         : None
*****************************************************************************************/
int32_t cb_GetGroupFileList(frame_head_t *p_cmd_head)
{
	int16_t Temp,id;
	int32_t file_counter;
	int32_t total_file_counter;
	file_info_t FileInfo;
	
	if(p_cmd_head->Proces == 0)
	{
		Temp = p_cmd_head->pDataBuf[0];
		if((Temp>=0)&&(Temp<=20))
		{
			
			total_file_counter = 0;

			for(id=0;id<DATA_RECODER_TOTAL_ITEM;id++)
			{
				if(file_info_simple[id].Used != 3)
				{
					continue;
				}
				total_file_counter++;	
			}
			
			file_counter = Mem_File_GetGroupFile(Temp,-1,0);
			
			p_cmd_head->pDataBuf[0] = (file_counter>>0)&0xFF;
			p_cmd_head->pDataBuf[1] = (file_counter>>8)&0xFF;
			
			p_cmd_head->pDataBuf[2] = (total_file_counter>>0)&0xFF;
			p_cmd_head->pDataBuf[3] = (total_file_counter>>8)&0xFF;
			
			p_cmd_head->pDataBuf[4] = (DATA_RECODER_TOTAL_ITEM>>0)&0xFF;
			p_cmd_head->pDataBuf[5] = (DATA_RECODER_TOTAL_ITEM>>8)&0xFF;
			
			p_cmd_head->pDataBuf[6] = (MAX_RECODER_GROUP_CNT>>0)&0xFF;
			p_cmd_head->pDataBuf[7] = 0;
		}
		
		p_cmd_head->DataLength = 8;
		p_cmd_head->DeviceStatus = CMD_SUCCESS;
		return 0;
	}
	
	else if(p_cmd_head->Proces == 1)
	{
		uint16_t group_id;
		uint16_t file_id;
		int32_t result ;
		
		group_id = p_cmd_head->pDataBuf[0];
		file_id = p_cmd_head->pDataBuf[2]|p_cmd_head->pDataBuf[3]<<8;
		

		result = Mem_File_GetGroupFile(group_id,file_id,0);
		if(result != -1) 
		{
			if(read_file_info_nocheck(result,&FileInfo)==0)
			{
			
				*(uint16_t*)(&p_cmd_head->pDataBuf[0]) = FileInfo.group;
				*(uint16_t*)(&p_cmd_head->pDataBuf[2]) = FileInfo.TagID;
				*(uint16_t*)(&p_cmd_head->pDataBuf[4]) = FileInfo.Meas_head.SamplingSpan;
				*(uint8_t*)(&p_cmd_head->pDataBuf[6]) = FileInfo.StartStatus;
				*(uint8_t*)(&p_cmd_head->pDataBuf[7]) = FileInfo.EndStatus;	
				*(uint32_t*)(&p_cmd_head->pDataBuf[8]) = FileInfo.Start_Date;
				*(uint32_t*)(&p_cmd_head->pDataBuf[12]) = FileInfo.End_Date;
				*(uint32_t*)(&p_cmd_head->pDataBuf[16]) = FileInfo.TotalItem;
				*(uint32_t*)(&p_cmd_head->pDataBuf[20]) = FileInfo.Meas_head.Start_Cond;
				*(uint32_t*)(&p_cmd_head->pDataBuf[24]) = FileInfo.Meas_head.Start_Params_1;
				*(uint32_t*)(&p_cmd_head->pDataBuf[28]) = FileInfo.Meas_head.Start_Params_2;
				*(uint32_t*)(&p_cmd_head->pDataBuf[32]) = FileInfo.Meas_head.Start_Params_3;
				*(uint32_t*)(&p_cmd_head->pDataBuf[36]) = FileInfo.Meas_head.Start_Params_4;
				*(uint32_t*)(&p_cmd_head->pDataBuf[40]) = FileInfo.Meas_head.Stop_Cond;
				*(uint32_t*)(&p_cmd_head->pDataBuf[44]) = FileInfo.Meas_head.Stop_Params_1;
				*(uint32_t*)(&p_cmd_head->pDataBuf[48]) = FileInfo.Meas_head.Stop_Params_2;
				*(uint32_t*)(&p_cmd_head->pDataBuf[52]) = FileInfo.Meas_head.Stop_Params_3;
								
								
				p_cmd_head->DataLength = 56;
				p_cmd_head->DeviceStatus = CMD_SUCCESS;
			}
			else
			{
				p_cmd_head->DataLength = 0;
				p_cmd_head->DeviceStatus = CMD_ERROR;
			}
		}
		else
		{
			p_cmd_head->DataLength = 0;
			p_cmd_head->DeviceStatus = CMD_SUCCESS;
		}
	}
	else
	{
		p_cmd_head->DataLength = 0;
		p_cmd_head->DeviceStatus = CMD_INVPARAMS;
		return -1;
	}
	
	return 0;
}



int32_t cb_DelGroupFile(frame_head_t *p_cmd_head)
{
	if(p_cmd_head->Proces == 0)
	{
		uint16_t group_id;
		uint16_t file_id;
		int32_t result ;
    
		group_id = p_cmd_head->pDataBuf[0]|p_cmd_head->pDataBuf[1]<<8;
		file_id = p_cmd_head->pDataBuf[2]|p_cmd_head->pDataBuf[3]<<8;
		
		result = Mem_File_GetGroupFile(group_id,file_id,0);
		if(result != -1) 
		{
			del_group_file_id(result);

			file_info_simple[result ].id= 0;
			file_info_simple[result ].Used= 0;
			file_info_simple[result ].group= 0;
			file_info_simple[result ].Start_Date= 0;

			p_cmd_head->DataLength = 0;
			p_cmd_head->DeviceStatus = CMD_SUCCESS;
		}
		else
		{
		  p_cmd_head->DataLength = 0;
		  p_cmd_head->DeviceStatus = CMD_INVPARAMS;
		  return -1;
		}
	}
	else
	{
		p_cmd_head->DataLength = 0;
		p_cmd_head->DeviceStatus = CMD_INVPARAMS;
		return -1;
	}
	
	return 0;
}

/*****************************************************************************************
* Function Name  : cb_GetGroupFileStream
* Description    : 
* Input          : None
* Output         : None
* Return         : None
*****************************************************************************************/
int32_t cb_GetGroupFileStream(frame_head_t *p_cmd_head)
{
	//int16_t Temp;
	//int32_t file_counter;
	//static file_info_t FileInfo;
	uint16_t group_id;
	uint16_t file_id;
	int32_t result ;
	
	
	if(p_cmd_head->Proces == 0)
	{
		group_id = p_cmd_head->pDataBuf[0]|p_cmd_head->pDataBuf[1]<<8;
		file_id = p_cmd_head->pDataBuf[2]|p_cmd_head->pDataBuf[3]<<8;
		
		result = Mem_File_GetGroupFile(group_id,file_id,0);
		
		//read_counter = 0;
		
		if(result == -1)
		{
			p_cmd_head->DataLength = 0;
			p_cmd_head->DeviceStatus = CMD_SUCCESS;
			return -1;
		}
		
	
		
		read_file_info_nocheck(result,&FileInfo);
		*(uint16_t*)(&p_cmd_head->pDataBuf[0]) = FileInfo.group;
		*(uint16_t*)(&p_cmd_head->pDataBuf[2]) = FileInfo.TagID;
		*(uint16_t*)(&p_cmd_head->pDataBuf[4]) = FileInfo.Meas_head.SamplingSpan;
		*(uint8_t*)(&p_cmd_head->pDataBuf[6]) = FileInfo.StartStatus;
		*(uint8_t*)(&p_cmd_head->pDataBuf[7]) = FileInfo.EndStatus;	
		*(uint32_t*)(&p_cmd_head->pDataBuf[8]) = FileInfo.Start_Date;
		*(uint32_t*)(&p_cmd_head->pDataBuf[12]) = FileInfo.End_Date;
		*(uint32_t*)(&p_cmd_head->pDataBuf[16]) = FileInfo.TotalItem;
		*(uint32_t*)(&p_cmd_head->pDataBuf[20]) = FileInfo.Meas_head.Start_Cond;
		*(uint32_t*)(&p_cmd_head->pDataBuf[24]) = FileInfo.Meas_head.Start_Params_1;
		*(uint32_t*)(&p_cmd_head->pDataBuf[28]) = FileInfo.Meas_head.Start_Params_2;
		*(uint32_t*)(&p_cmd_head->pDataBuf[32]) = FileInfo.Meas_head.Start_Params_3;
		*(uint32_t*)(&p_cmd_head->pDataBuf[36]) = FileInfo.Meas_head.Start_Params_4;
		*(uint32_t*)(&p_cmd_head->pDataBuf[40]) = FileInfo.Meas_head.Stop_Cond;
		*(uint32_t*)(&p_cmd_head->pDataBuf[44]) = FileInfo.Meas_head.Stop_Params_1;
		*(uint32_t*)(&p_cmd_head->pDataBuf[48]) = FileInfo.Meas_head.Stop_Params_2;
		*(uint32_t*)(&p_cmd_head->pDataBuf[52]) = FileInfo.Meas_head.Stop_Params_3;
		
		
		if(Mem_File_ReadFileInit(&FileInfo)!=0)
		{
			//pFileInfo = 0;
			p_cmd_head->DataLength = 0;
			p_cmd_head->DeviceStatus = CMD_SUCCESS;
			return -1;
		}
		
		p_cmd_head->DataLength = 56;
		p_cmd_head->DeviceStatus = CMD_SUCCESS;
		
		return 0;
	}
	else if(p_cmd_head->Proces == 1)
	{
		int readlength;
		int offset;	
		int length;
		
		offset = p_cmd_head->pDataBuf[0];
		offset |= p_cmd_head->pDataBuf[1]<<8;
		offset |= p_cmd_head->pDataBuf[2]<<16;
		offset |= p_cmd_head->pDataBuf[3]<<24;
		
		length = p_cmd_head->pDataBuf[4];
		length |= p_cmd_head->pDataBuf[5]<<8;
		length |= p_cmd_head->pDataBuf[6]<<16;
		length |= p_cmd_head->pDataBuf[7]<<24;
			
		readlength = Mem_File_ReadFile(&FileInfo,offset,length,p_cmd_head->pDataBuf);
		p_cmd_head->DataLength = readlength;
		p_cmd_head->DeviceStatus = CMD_SUCCESS;
		return 0;
	}
	else
	{
		p_cmd_head->DataLength = 0;
		p_cmd_head->DeviceStatus = CMD_INVPARAMS;
		return -1;
	}
	
}

/*****************************************************************************************
* Function Name  : cb_GetGroupBigFileStream
* Description    : 
* Input          : None
* Output         : None
* Return         : None
*****************************************************************************************/
int32_t cb_GetGroupBigFileStream(frame_head_t *p_cmd_head)
{
	//int16_t Temp;
	//int32_t file_counter;
	//static file_info_t FileInfo;
	uint16_t group_id;
	uint16_t file_id;
	int32_t result ;
	
	
	if(p_cmd_head->Proces == 0)
	{
		group_id = p_cmd_head->pDataBuf[0]|p_cmd_head->pDataBuf[1]<<8;  
		file_id = p_cmd_head->pDataBuf[2]|p_cmd_head->pDataBuf[3]<<8;
		
		result = Mem_File_GetGroupFile(group_id,file_id,0);
		
		//read_counter = 0;
		
		if(result == -1)
		{
			p_cmd_head->DataLength = 0;
			p_cmd_head->DeviceStatus = CMD_SUCCESS;
			return -1;
		}
		
	
		
		read_file_info_nocheck(result,&FileInfo);
		*(uint16_t*)(&p_cmd_head->pDataBuf[0]) = FileInfo.group;
		*(uint16_t*)(&p_cmd_head->pDataBuf[2]) = FileInfo.TagID;
		*(uint16_t*)(&p_cmd_head->pDataBuf[4]) = FileInfo.Meas_head.SamplingSpan;
		*(uint8_t*)(&p_cmd_head->pDataBuf[6]) = FileInfo.StartStatus;
		*(uint8_t*)(&p_cmd_head->pDataBuf[7]) = FileInfo.EndStatus;	
		*(uint32_t*)(&p_cmd_head->pDataBuf[8]) = FileInfo.Start_Date;
		*(uint32_t*)(&p_cmd_head->pDataBuf[12]) = FileInfo.End_Date;
		*(uint32_t*)(&p_cmd_head->pDataBuf[16]) = FileInfo.TotalItem;
		*(uint32_t*)(&p_cmd_head->pDataBuf[20]) = FileInfo.Meas_head.Start_Cond;
		*(uint32_t*)(&p_cmd_head->pDataBuf[24]) = FileInfo.Meas_head.Start_Params_1;
		*(uint32_t*)(&p_cmd_head->pDataBuf[28]) = FileInfo.Meas_head.Start_Params_2;
		*(uint32_t*)(&p_cmd_head->pDataBuf[32]) = FileInfo.Meas_head.Start_Params_3;
		*(uint32_t*)(&p_cmd_head->pDataBuf[36]) = FileInfo.Meas_head.Start_Params_4;
		*(uint32_t*)(&p_cmd_head->pDataBuf[40]) = FileInfo.Meas_head.Stop_Cond;
		*(uint32_t*)(&p_cmd_head->pDataBuf[44]) = FileInfo.Meas_head.Stop_Params_1;
		*(uint32_t*)(&p_cmd_head->pDataBuf[48]) = FileInfo.Meas_head.Stop_Params_2;
		*(uint32_t*)(&p_cmd_head->pDataBuf[52]) = FileInfo.Meas_head.Stop_Params_3;
		
		
		if(Mem_File_ReadFileInit(&FileInfo)!=0)
		{
			//pFileInfo = 0;
			p_cmd_head->DataLength = 0;
			p_cmd_head->DeviceStatus = CMD_SUCCESS;
			return -1;
		}
		
		p_cmd_head->DataLength = 56;
		p_cmd_head->DeviceStatus = CMD_SUCCESS;
		
		return 0;
	}
	else if(p_cmd_head->Proces == 1)
	{
		int readlength;
		int offset;	
		int length;
		
		offset = p_cmd_head->pDataBuf[0];
		offset |= p_cmd_head->pDataBuf[1]<<8;
		offset |= p_cmd_head->pDataBuf[2]<<16;
		offset |= p_cmd_head->pDataBuf[3]<<24;
		
		length = p_cmd_head->pDataBuf[4];
		length |= p_cmd_head->pDataBuf[5]<<8;
		length |= p_cmd_head->pDataBuf[6]<<16;
		length |= p_cmd_head->pDataBuf[7]<<24;
			
		readlength = Mem_File_ReadFile(&FileInfo,offset,length * 2,p_cmd_head->pDataBuf);
		p_cmd_head->DataLength = readlength / 2;
//		readlength = Mem_File_ReadFile(&FileInfo,offset,length,p_cmd_head->pDataBuf);
//		p_cmd_head->DataLength = readlength;
		p_cmd_head->DeviceStatus = CMD_SUCCESS;
		return 0;
	}
	else
	{
		p_cmd_head->DataLength = 0;
		p_cmd_head->DeviceStatus = CMD_INVPARAMS;
		return -1;
	}
	
}
//
int last_add;
/*****************************************************************************************
* Function Name  : cb_FileStreamFastRead
* Description    : 
* Input          : None
* Output         : None
* Return         : None
*****************************************************************************************/
//int32_t cb_FileStreamFastRead(frame_head_t *p_cmd_head)
//{
//	//int32_t file_counter;
//	int16_t status;
//	//static file_info_t FileInfo;
//	uint16_t group_id;
//	uint16_t file_id;
//	int32_t result;
//	//static uint32_t read_counter;
//	
//	status = 0;
//	
//	if(p_cmd_head->Proces == 0)
//	{
//		group_id = p_cmd_head->pDataBuf[0]|p_cmd_head->pDataBuf[1]<<8;
//		file_id = p_cmd_head->pDataBuf[2]|p_cmd_head->pDataBuf[3]<<8;
//		
//		result = Mem_File_GetGroupFile(group_id,file_id,0);
//		
//		if(result == -1)
//		{
//			p_cmd_head->DataLength = 0;
//			p_cmd_head->DeviceStatus = CMD_SUCCESS;
//			status = -1;
//			goto end;
//		}
//		
//		read_file_info_nocheck(result,&FileInfo);
//		*(uint16_t*)(&p_cmd_head->pDataBuf[0]) = FileInfo.group;
//		*(uint16_t*)(&p_cmd_head->pDataBuf[2]) = FileInfo.TagID;
//		*(uint16_t*)(&p_cmd_head->pDataBuf[4]) = FileInfo.Meas_head.SamplingSpan;
//		*(uint8_t*)(&p_cmd_head->pDataBuf[6]) = FileInfo.StartStatus;
//		*(uint8_t*)(&p_cmd_head->pDataBuf[7]) = FileInfo.EndStatus;	
//		*(uint32_t*)(&p_cmd_head->pDataBuf[8]) = FileInfo.Start_Date;
//		*(uint32_t*)(&p_cmd_head->pDataBuf[12]) = FileInfo.End_Date;
//		*(uint32_t*)(&p_cmd_head->pDataBuf[16]) = FileInfo.TotalItem;
//		*(uint32_t*)(&p_cmd_head->pDataBuf[20]) = FileInfo.Meas_head.Start_Cond;
//		*(uint32_t*)(&p_cmd_head->pDataBuf[24]) = FileInfo.Meas_head.Start_Params_1;
//		*(uint32_t*)(&p_cmd_head->pDataBuf[28]) = FileInfo.Meas_head.Start_Params_2;
//		*(uint32_t*)(&p_cmd_head->pDataBuf[32]) = FileInfo.Meas_head.Start_Params_3;
//		*(uint32_t*)(&p_cmd_head->pDataBuf[36]) = FileInfo.Meas_head.Start_Params_4;
//		*(uint32_t*)(&p_cmd_head->pDataBuf[40]) = FileInfo.Meas_head.Stop_Cond;
//		*(uint32_t*)(&p_cmd_head->pDataBuf[44]) = FileInfo.Meas_head.Stop_Params_1;
//		*(uint32_t*)(&p_cmd_head->pDataBuf[48]) = FileInfo.Meas_head.Stop_Params_2;
//		*(uint32_t*)(&p_cmd_head->pDataBuf[52]) = FileInfo.Meas_head.Stop_Params_3;
//		
//		if(Mem_File_ReadFileInit(&FileInfo)!=0)
//		{
//			p_cmd_head->DataLength = 0;
//			p_cmd_head->DeviceStatus = CMD_SUCCESS;
//			
//			status = -1;
//			goto end;
//		}
//		
//		p_cmd_head->DataLength = 56;
//		p_cmd_head->DeviceStatus = CMD_SUCCESS;
//	}
//	else if(p_cmd_head->Proces == 1)
//	{
//		int readlength;
//		int offset;	
//		
//		offset = p_cmd_head->pDataBuf[0];
//		offset |= p_cmd_head->pDataBuf[1]<<8;
//		offset |= p_cmd_head->pDataBuf[2]<<16;
//		offset |= p_cmd_head->pDataBuf[3]<<24;
//			
//		last_add = offset;
//		
//		readlength = Mem_File_ReadFile(&FileInfo,offset,48,p_cmd_head->pDataBuf);
//		
//		Mem_File_ReadFile(&FileInfo,offset + 48,208,simpleProfile_RealTime_Buf);
//    
//		p_cmd_head->DataLength = readlength;
//		p_cmd_head->DeviceStatus = CMD_SUCCESS;
//	}
//	else
//	{
//		p_cmd_head->DataLength = 0;
//		p_cmd_head->DeviceStatus = CMD_INVPARAMS;
//		
//		status = -1;
//		goto end;
//	}
//
//end:	
//	return status;
//}

/*****************************************************************************************
* Function Name  : cb_MeasControl
* Description    : 
* Input          : None
* Output         : None
* Return         : None
*****************************************************************************************/
int32_t cb_MeasControl(frame_head_t *p_cmd_head)
{
	if(p_cmd_head->Proces  != 0)
	{
		if(p_cmd_head->Proces  == COMMAND_CTRL_START)
		{
			iProfile.cmd_ctrl.Meas_ctrl = COMMAND_CTRL_START;
			ESP_LOGI("TAG", "COMMAND_CTRL_START");
		}
		else
		{
			if(p_cmd_head->Proces  == COMMAND_CTRL_COND_START)
			{
				iProfile.meas_params_cur.SamplingSpan=		   *(uint32_t*)(&p_cmd_head->pDataBuf[0]);
				iProfile.meas_params_cur.Start_Cond=           *(uint32_t*)(&p_cmd_head->pDataBuf[4]);
				iProfile.meas_params_cur.Start_Params_1=       *(uint32_t*)(&p_cmd_head->pDataBuf[8]);
				iProfile.meas_params_cur.Start_Params_2=       *(uint32_t*)(&p_cmd_head->pDataBuf[12]);
				iProfile.meas_params_cur.Start_Params_3=       *(uint32_t*)(&p_cmd_head->pDataBuf[16]);
				iProfile.meas_params_cur.Start_Params_4=       *(uint32_t*)(&p_cmd_head->pDataBuf[20]);
				iProfile.meas_params_cur.Stop_Cond=            *(uint32_t*)(&p_cmd_head->pDataBuf[24]);
				iProfile.meas_params_cur.Stop_Params_1=        *(uint32_t*)(&p_cmd_head->pDataBuf[28]);
				iProfile.meas_params_cur.Stop_Params_2=        *(uint32_t*)(&p_cmd_head->pDataBuf[32]);
				iProfile.meas_params_cur.Stop_Params_3=        *(uint32_t*)(&p_cmd_head->pDataBuf[36]);
				
				iProfile.sys_status.Group = 					p_cmd_head->pDataBuf[40];
				
				iProfile.cmd_ctrl.Meas_ctrl =                   COMMAND_CTRL_COND_START;
				ESP_LOGI("TAG", "COMMAND_CTRL_COND_START");
			}
			else if(p_cmd_head->Proces  == COMMAND_CTRL_DIRECT_START)
			{
				iProfile.meas_params_cur.SamplingSpan=		*(uint32_t*)(&p_cmd_head->pDataBuf[0]);
				iProfile.meas_params_cur.Start_Cond=          *(uint32_t*)(&p_cmd_head->pDataBuf[4]);
				iProfile.meas_params_cur.Start_Params_1=        *(uint32_t*)(&p_cmd_head->pDataBuf[8]);
				iProfile.meas_params_cur.Start_Params_2=       *(uint32_t*)(&p_cmd_head->pDataBuf[12]);
				iProfile.meas_params_cur.Start_Params_3=       *(uint32_t*)(&p_cmd_head->pDataBuf[16]);
				iProfile.meas_params_cur.Start_Params_4=       *(uint32_t*)(&p_cmd_head->pDataBuf[20]);
				iProfile.meas_params_cur.Stop_Cond=            *(uint32_t*)(&p_cmd_head->pDataBuf[24]);
				iProfile.meas_params_cur.Stop_Params_1=       *(uint32_t*)(&p_cmd_head->pDataBuf[28]);
				iProfile.meas_params_cur.Stop_Params_2=        *(uint32_t*)(&p_cmd_head->pDataBuf[32]);
				iProfile.meas_params_cur.Stop_Params_3=        *(uint32_t*)(&p_cmd_head->pDataBuf[36]);
				
				iProfile.sys_status.Group = 				    p_cmd_head->pDataBuf[40];
				
				iProfile.cmd_ctrl.Meas_ctrl = COMMAND_CTRL_DIRECT_START;
				ESP_LOGI("TAG", "COMMAND_CTRL_DIRECT_START");
			}
			else if(p_cmd_head->Proces  == COMMAND_CTRL_STOP)
			{
				iProfile.cmd_ctrl.Meas_ctrl = COMMAND_CTRL_STOP;
				ESP_LOGI("TAG", "COMMAND_CTRL_STOP");
			}
		}
	}
	
	
	p_cmd_head->pDataBuf[0] = iProfile.cmd_ctrl.Status;	//RunStatus
	p_cmd_head->pDataBuf[1] = iProfile.cmd_ctrl.cond_stage;
	p_cmd_head->pDataBuf[2] = iProfile.cmd_ctrl.Temp_ch;
	p_cmd_head->pDataBuf[3] = 0;//res
	
	*(uint16_t*)(&p_cmd_head->pDataBuf[4]) = iProfile.cmd_ctrl.Temp_value;
	*(uint16_t*)(&p_cmd_head->pDataBuf[6]) = iProfile.cmd_ctrl.Time_value;
	
	*(uint32_t*)(&p_cmd_head->pDataBuf[8]) = iProfile.cmd_ctrl.Recorder_cnt;
	
	p_cmd_head->DataLength = 16;
	p_cmd_head->DeviceStatus = CMD_SUCCESS;	
	
	return 0;
}
//


/*****************************************************************************************
* Function Name  : cb_GetMemory
* Description    : 
* Input          : None
* Output         : None
* Return         : None
*****************************************************************************************/
file_info_t file_info_buffer_temp;

int32_t cb_GetMemory(frame_head_t *p_cmd_head)
{
	int result;
	uint16_t file_id;
	uint32_t offset;
	result = 0;
	if(p_cmd_head->Proces  == 0)	//Get Current Meas File ID
	{
		if(iProfile.cmd_ctrl.Status != COMMAND_CTRL_STS_MEAS)
		{
			p_cmd_head->pDataBuf[0] = 0;
			p_cmd_head->pDataBuf[1] = 0;
			p_cmd_head->pDataBuf[2] = 0;
			p_cmd_head->pDataBuf[3] = 0;
		}
		else
		{
			p_cmd_head->pDataBuf[0] = 1;
			p_cmd_head->pDataBuf[1] = 0;
			*(uint16_t*)(&p_cmd_head->pDataBuf[2]) = iProfile.sys_status.MeasFileID;
		}
		
		p_cmd_head->DataLength = 4;
		p_cmd_head->DeviceStatus = CMD_SUCCESS;	
	}
	else if(p_cmd_head->Proces  == 1)	//Request FileHeader
	{
		file_id = *(uint16_t*)(&p_cmd_head->pDataBuf[0]);
   
		if(read_file_info_nocheck(file_id,&file_info_buffer_temp)<0)
		{
			p_cmd_head->DataLength = 0;
			p_cmd_head->DeviceStatus = CMD_INVPARAMS;
		}
		
		Mem_File_ReadFile(&file_info_buffer_temp,0,48,p_cmd_head->pDataBuf);	
		Mem_File_ReadFile(&file_info_buffer_temp,48,208,simpleProfile_RealTime_Buf);
		p_cmd_head->DataLength = 48;
		p_cmd_head->DeviceStatus = CMD_SUCCESS;	

	}
	else if(p_cmd_head->Proces  == 2)	//Request File buf
	{	
		offset = p_cmd_head->pDataBuf[0];
		offset |= p_cmd_head->pDataBuf[1]<<8;
		offset |= p_cmd_head->pDataBuf[2]<<16;
		offset |= p_cmd_head->pDataBuf[3]<<24;
			
		offset = offset*32 + 256;
    
		Mem_File_ReadFile(&file_info_buffer_temp,offset,48,p_cmd_head->pDataBuf);
		Mem_File_ReadFile(&file_info_buffer_temp,offset + 48,208,simpleProfile_RealTime_Buf);
		p_cmd_head->DataLength = 48;
		p_cmd_head->DeviceStatus = CMD_SUCCESS;
	}
	else if(p_cmd_head->Proces  == 3)	//Request File buf
	{	
		uint16_t seek;
		uint16_t i;
		offset = p_cmd_head->pDataBuf[0];
		if(offset > 3)
			offset = 3;
			
		seek = offset * 52;
		
		for(i=0;i<52;i++)
		{
			p_cmd_head->pDataBuf[i] = simpleProfile_RealTime_Buf[seek + i];
		}
    
		p_cmd_head->DataLength = 52;
		p_cmd_head->DeviceStatus = CMD_SUCCESS;
	}
	else if(p_cmd_head->Proces  == 4)	//Request File buf
	{	
		uint16_t seek;
		uint16_t i;
		offset = p_cmd_head->pDataBuf[0];
		if(offset > 3)
			offset = 3;
			
		seek = offset * 52;
		
		for(i=0;i<52;i++)
		{
			p_cmd_head->pDataBuf[i] = simpleProfile_RealTime_Buf[seek + i];
		}
    
		p_cmd_head->DataLength = 52;
		p_cmd_head->DeviceStatus = CMD_SUCCESS;
	}
	return result;
}

int32_t cb_GetMemory_usb(frame_head_t *p_cmd_head)
{
	uint16_t file_id;
	uint32_t offset;

	if(p_cmd_head->Proces  == 0)	//Get Current Meas File ID
	{
		if(iProfile.cmd_ctrl.Status != COMMAND_CTRL_STS_MEAS)
		{
			p_cmd_head->pDataBuf[0] = 0;
			p_cmd_head->pDataBuf[1] = 0;
			p_cmd_head->pDataBuf[2] = 0;
			p_cmd_head->pDataBuf[3] = 0;
		}
		else
		{
			p_cmd_head->pDataBuf[0] = 1;
			p_cmd_head->pDataBuf[1] = 0;
			*(uint16_t*)(&p_cmd_head->pDataBuf[2]) = iProfile.sys_status.MeasFileID;
		}
		
		p_cmd_head->DataLength = 4;
		p_cmd_head->DeviceStatus = CMD_SUCCESS;	
	}
	else if(p_cmd_head->Proces  == 1)	//Request FileHeader
	{
		file_id = *(uint16_t*)(&p_cmd_head->pDataBuf[0]);
   
		if(read_file_info_nocheck(file_id,&file_info_buffer_temp)<0)
		{
			p_cmd_head->DataLength = 0;
			p_cmd_head->DeviceStatus = CMD_INVPARAMS;
		}
		
		Mem_File_ReadFile(&file_info_buffer_temp,0,48,p_cmd_head->pDataBuf);	
		Mem_File_ReadFile(&file_info_buffer_temp,48,208,simpleProfile_RealTime_Buf);
		p_cmd_head->DataLength = 48;
		p_cmd_head->DeviceStatus = CMD_SUCCESS;	
	}
	else if(p_cmd_head->Proces  == 2)	//Request File buf
	{	
		offset = p_cmd_head->pDataBuf[0];
		offset |= p_cmd_head->pDataBuf[1]<<8;
		offset |= p_cmd_head->pDataBuf[2]<<16;
		offset |= p_cmd_head->pDataBuf[3]<<24;
			
		offset = offset*32 + 256;
    
		Mem_File_ReadFile(&file_info_buffer_temp,offset,32,p_cmd_head->pDataBuf);
		
    
		p_cmd_head->DataLength = 32;
		p_cmd_head->DeviceStatus = CMD_SUCCESS;
	}
	return 0;
}

extern int32_t sen_heat_res;
extern uint16_t sensor_routine_1;
extern uint16_t sensor_instatus;
/*****************************************************************************************
* Function Name  : cb_get_dev_info
* Description    : 
* Input          : None
* Output         : None
* Return         : None
*****************************************************************************************/
int32_t cb_get_raw_data(frame_head_t *p_cmd_head)
{
	uint8_t i;

	int32_t *pdwBuf;
  
	pdwBuf = (int32_t*)&p_cmd_head->pDataBuf[0];
	i=0;

	pdwBuf[i++] = iProfile.vibrate.x_raw;
	pdwBuf[i++] = iProfile.vibrate.y_raw;
	pdwBuf[i++] = iProfile.vibrate.z_raw;   
	pdwBuf[i++] = iProfile.speed.s1_raw;
	pdwBuf[i++] = iProfile.speed.s2_raw;
	
	pdwBuf[i++] = iProfile.oxygen.sensor_out;    
	pdwBuf[i++] = iProfile.pressure.pressure_data_raw;
	
	pdwBuf[i++] = (sensor_routine_1<<24)|(sensor_instatus<<16)|(sen_heat_res&0xFFFF);
    
    pdwBuf[i++] = (iProfile.vibrate.pitch_raw <<16)|((iProfile.vibrate.roll_raw<<0)&0xFFFF);

	p_cmd_head->DataLength = 56;
	p_cmd_head->DeviceStatus = CMD_SUCCESS;

	return 0;
}
//

/*****************************************************************************************
* Function Name  : cb_get_dev_info
* Description    : 
* Input          : None
* Output         : None
* Return         : None
*****************************************************************************************/
int32_t cb_log_opera(frame_head_t *p_cmd_head)
{
    uint32_t temp_u32;
    uint32_t temp_u32_a;
	if(p_cmd_head->Proces  == 0)	//get log length
	{	
        temp_u32 = ef_log_get_used_size();
        
        p_cmd_head->pDataBuf[0] = (temp_u32>>0)&0xFF;
        p_cmd_head->pDataBuf[1] = (temp_u32>>8)&0xFF;
        p_cmd_head->pDataBuf[2] = (temp_u32>>16)&0xFF;
        p_cmd_head->pDataBuf[3] = (temp_u32>>24)&0xFF;      		
    
		p_cmd_head->DataLength = 4;
		p_cmd_head->DeviceStatus = CMD_SUCCESS;
	}
    else if(p_cmd_head->Proces  == 1)	//get log
    {
        temp_u32_a = ef_log_get_used_size();
        temp_u32 = (p_cmd_head->pDataBuf[0]<<0)|(p_cmd_head->pDataBuf[1]<<8)|(p_cmd_head->pDataBuf[2]<<16)|(p_cmd_head->pDataBuf[3]<<24);
        if((temp_u32+32)>temp_u32_a)
        {
            p_cmd_head->DataLength = 0;
            p_cmd_head->DeviceStatus = CMD_SUCCESS;
        }
        else
        {
            ef_log_read(temp_u32,(uint32_t*)p_cmd_head->pDataBuf,32);
        
            p_cmd_head->DataLength = 32;
            p_cmd_head->DeviceStatus = CMD_SUCCESS;
        }       
    }
    else if(p_cmd_head->Proces  == 10)	//log clean
    {
        if((p_cmd_head->pDataBuf[0]==0xAA)&&
            (p_cmd_head->pDataBuf[1]==0x55)&&
            (p_cmd_head->pDataBuf[2]==0xCC)&&
            (p_cmd_head->pDataBuf[3]==0xBB))
        {
            ef_log_clean();
        }
        p_cmd_head->DataLength = 0;
        p_cmd_head->DeviceStatus = CMD_SUCCESS;
    }

        
	return 0;
}
//


void system_restore_factory(void)
{
	//DeviceResetDeviceInfo();
	erase_meas_info();
	erase_all_headinfo();
	//Flash_If_Erase(DEVICE_CALCULATE_ADDR,1);
}

void system_test_create_memory(void)
{
	//DeviceResetDeviceInfo();
	erase_meas_info();
	erase_all_headinfo();
	Mem_FileHead_Init();
	//Mem_File_Create_Test_Recoder();
	//Flash_If_Erase(DEVICE_CALCULATE_ADDR,1);
}

void meas_params_init(MEAS_PARAMS *pmeas_params)
{
    pmeas_params->SamplingSpan = 0;
    pmeas_params->Start_Cond = 0;
    pmeas_params->Start_Params_1 = 0;
    pmeas_params->Start_Params_2 = 0;
    pmeas_params->Start_Params_3 = 0;
    pmeas_params->Start_Params_4 = 0;
    pmeas_params->Stop_Cond = 0;
    pmeas_params->Stop_Params_1 = 0;
    pmeas_params->Stop_Params_2 = 0;
    pmeas_params->Stop_Params_3 = 0;
}

uint8_t write_test_memory;
void Task_iProfile(void *Parameter)
{
    // RTC_TimeTypeDef sTime = {0};
	// RTC_DateTypeDef sDate = {0};
    calibre_timer_flag = 0;
	write_test_memory = 0;
    Reset_system_status = 0;
    IntoBootMode_flag = 0;
	while(1)
	{
		xSemaphoreTake(sem_iprofile,portMAX_DELAY);

		if(calibre_timer_flag!=0)
		{  

			VCAM_RTC.tm_year	= calib_year+100;
			VCAM_RTC.tm_mon		= calib_month-1;
			VCAM_RTC.tm_mday	= calib_day;
			VCAM_RTC.tm_hour	= calib_hour;
			VCAM_RTC.tm_min		= calib_minute;
			VCAM_RTC.tm_sec		= calib_second;

			/* 获取1970.1.1以来的总秒数 */
			time_t epoch  = mktime(&VCAM_RTC);

			struct timeval val = { .tv_sec = epoch , .tv_usec = 0 };
			/* 设置当前时间 */
			settimeofday(&val, NULL);

//     HAL_RTCEx_SetSmoothCalib(&VCAM_RTC,RTC_SMOOTHCALIB_PERIOD_32SEC,RTC_SMOOTHCALIB_PLUSPULSES_RESET,134);				  

            
            RTC_BKP = 0x33AACC55;
			
            calibre_timer_flag = 0;
			continue;
		}
		
		if(Reset_system_status !=0)
		{
			if(Reset_system_status==1)
			{
				Reset_system_status = 0;
				vTaskDelay(pdMS_TO_TICKS(100));
				esp_restart();
				while(1);
			}
			else if(Reset_system_status==2)
			{
				system_restore_factory();
				Reset_system_status = 0;
				
				if(write_test_memory != 0)
				{
					write_test_memory = 0;
					system_test_create_memory();				
				}
				
				vTaskDelay(pdMS_TO_TICKS(100));
				esp_restart();
			}
		}
		
		if(IntoBootMode_flag != 0)
		{	
			IntoBootMode_flag = 0;
			NVM_SetIntoBootMode();
			
			vTaskDelay(pdMS_TO_TICKS(100));
			
			esp_restart();
			while(1);
		}		
		vTaskDelay(pdMS_TO_TICKS(500));
	}
}


uint8_t NVM_Buf[64];
/*******************************************************************************
* Function Name  : NVM_SetIntoBootMode
* Description    : 
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
int NVM_SetIntoBootMode(void)
{
	int32_t status;
	uint16_t crc;
	uint16_t i;
	uint32_t *p32;
	uint32_t *pBuf;
//	uint8_t WriteBuf[NVM_PL_IntoBootModeFlag];
//	
//	p32 = (uint32_t *)NVM_PARAMS_ADDR;
//	pBuf = (uint32_t*)NVM_Buf;
//	for(i=0;i<16;i++)
//	{
//		pBuf[i] = p32[i];
//	}
//	
//	WriteBuf[0] = 0x33;
//	WriteBuf[1] = 0x99;
//	WriteBuf[2] = 0x77;
//	WriteBuf[3] = 0x66;
//	
//	for(i=0;i<NVM_PL_IntoBootModeFlag;i++)
//	{
//		NVM_Buf[NVM_P_IntoBootModeFlag+i] = WriteBuf[i];
//	}
//	crc = CRC16Software(NVM_Buf,62);
//	NVM_Buf[62] = (crc>>0)&0xFF;
//	NVM_Buf[63] = (crc>>8)&0xFF;
//	
//	status = FlashBlockErase(NVM_PARAMS_ADDR);
//    if( status != SUCCESS ) 
//		return status;
//	
//	p32 = (uint32_t *)NVM_PARAMS_ADDR;
//	pBuf = (uint32_t*)NVM_Buf;
//	for(i=0; i<16; i++)
//    {
//		status = FlashWriteDW( (UINT32)p32++, pBuf[i]);			
//        if( status !=SUCCESS )  
//        {
//            return status;
//        }            
//    }
	return 0;
}

/*******************************************************************************
* Function Name  : BSP_BootloaderMode
* Description    : 
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
int NVM_ResetIntoBootMode(void)
{
	int32_t status;
	uint16_t crc;
	uint16_t i;
	uint32_t *p32;
	uint32_t *pBuf;
//	uint8_t WriteBuf[NVM_PL_IntoBootModeFlag];
//	
//	p32 = (uint32_t *)NVM_PARAMS_ADDR;
//	pBuf = (uint32_t*)NVM_Buf;
//	for(i=0;i<16;i++)
//	{
//		pBuf[i] = p32[i];
//	}
//	
//	WriteBuf[0] = 0x00;
//	WriteBuf[1] = 0x00;
//	WriteBuf[2] = 0x00;
//	WriteBuf[3] = 0x00;
//	
//	for(i=0;i<NVM_PL_IntoBootModeFlag;i++)
//	{
//		NVM_Buf[NVM_P_IntoBootModeFlag+i] = WriteBuf[i];
//	}
//	crc = CRC16Software(NVM_Buf,62);
//	pBuf[62] = (crc>>0)&0xFF;
//	pBuf[63] = (crc>>8)&0xFF;
//	
//	status = FlashBlockErase(NVM_PARAMS_ADDR);
//    if( status != SUCCESS ) 
//		return status;
//	
//	p32 = (uint32_t *)NVM_PARAMS_ADDR;
//	pBuf = (uint32_t*)NVM_Buf;
//	for(i=0; i<16; i++)
//    {
//		status = FlashWriteDW( (UINT32)p32++, pBuf[i]);			
//        if( status !=SUCCESS )  
//        {
//            return status;
//        }            
//    }
	return 0;
}

void RTC_Timer_Init(void)
{
//	R8_SAFE_ACCESS_SIG = 0x57; 
//	R8_SAFE_ACCESS_SIG = 0xa8;

//	R8_CK32K_CONFIG    |=  RB_CLK_XT32K_PON | RB_CLK_INT32K_PON | RB_CLK_OSC32K_XT  ;
//	R8_XT32K_TUNE = 0x43; //LSE 200%
//	DelayMs(100);
//	R8_SAFE_ACCESS_SIG = 0x57; 
//	R8_SAFE_ACCESS_SIG = 0xa8;
//	R8_XT32K_TUNE = 0x41; //LSE 100%
//	R8_SAFE_ACCESS_SIG = 0;
//	RTC_InitTime( 2024, 1, 22, 22, 22, 22 );    //
}
//


void key_power_on(void)
{
	uint16_t i;
	uint8_t KeyTimer;
	
    //WWDG_SetCounter(0);	

	KeyTimer = 0;
	
#if (DEV_TYPE == DEV_TYPE_I7)||(DEV_TYPE == DEV_TYPE_XP)

	for(i=0;i<40;i++)
	{	
		if((KEY_POWER_STS()!=0)||(BUS_LINK_STS()!=0))
		{
			KeyTimer++;
		}
		vTaskDelay(pdMS_TO_TICKS(10));    /* 延时10ms */;
	}
		
#elif (DEV_TYPE == DEV_TYPE_MINI)
	
	for(i=0;i<40;i++)
	{	
		if((KEY_POWER_STS()!=0)||(BUS_LINK_STS()!=0))
		{
			KeyTimer++;
		}
		vTaskDelay(pdMS_TO_TICKS(10));    /* 延时10ms */;
	}
		
#elif (DEV_TYPE == DEV_TYPE_VACUUM)
	
//	for(i=0;i<40;i++)
//	{	
//		if((KEY_POWER_STS()!=0)||(BUS_LINK_STS()!=0))
//		{
//			KeyTimer++;
//		}
//		vTaskDelay(pdMS_TO_TICKS(10));    /* 延时10ms */;
//	}

    KeyTimer = 50;
    
#elif (DEV_TYPE == DEV_TYPE_OXYGEN_STD)

    for(i=0;i<40;i++)
	{	
		//WWDG_SetCounter(0);
		if((KEY_POWER_STS()!=0)||(BUS_LINK_STS()!=0))
		{
			KeyTimer++;
		}
		vTaskDelay(pdMS_TO_TICKS(10));    /* 延时10ms */;
	}
    
#elif (DEV_TYPE == DEV_TYPE_RPT_STD)

	for(i=0;i<40;i++)
	{	
		if((KEY_POWER_STS()!=0)||(BUS_LINK_STS()!=0))
		{
			KeyTimer++;
		}
		vTaskDelay(pdMS_TO_TICKS(10));    /* 延时10ms */;
	}
#else
#error
#endif

	if((KeyTimer<30))
    {
        // Power_OFF();
    }		
}



static void cb_tm_width(void *parameter)
{
	// AS5311_polling();
}

/*****************************************************************************************
* Function Name  : led_Polling
* Description    : 
* Input          : None
* Output         : None
* Return         : None
*****************************************************************************************/
void OLED_Init(void);
int iProfile_main_start(void)
{
    int32_t temp_i32;
    iProfile.sys_status.Error_id = 0;
    
    if(SerialFLASH_initialize() != 0)
    {
        iProfile.sys_status.Error_id |= ERROR_FLAG_MEMORY_DET;
    }

    log_write(EFLOG_LVL_INFO,LOG_TAG_POWER,LOG_POWER_ON,0,0,0,0);
      
	WDG_Refresh();	
	Mem_device_config_Load();

	// PowerManager_Start();

	Mem_MeasParams_Load();
    
	nvm_thermo_calib_Load();
    
    WDG_Refresh();	
	
	nvm_calib_pressure_Load();
	
	nvm_calib_oxygen_Load();
    
    nvm_calib_angle_Load();
    
	Mem_FileHead_Init();
    
	WDG_Refresh();	
    
    MEAS_Start();
    
#if (USE_V_OXY_1 != 0)
    O2Sensor_Start();
#endif
	
    
//#if (USE_V_WIDTH_1 != 0)
    
//    rt_timer_init(&timer_width_poll,"tm_width",
//					cb_tm_width,
//					RT_NULL,
//					2,
//					RT_TIMER_FLAG_PERIODIC | RT_TIMER_FLAG_HARD_TIMER);
//                    
//     rt_timer_start(&timer_width_poll);	           
//#endif
	sem_iprofile = xSemaphoreCreateCountingStatic(10,0,&xsem_iprofile);
    if (sem_iprofile == NULL)
    {
        // ESP_LOGE("iProfile_main_start","iProfile_main_start: sem_iprofile create failed");
    }
        
    // 创建静态任务
    iprofile_thread = xTaskCreateStatic(
        Task_iProfile,        				// 任务函数
        "iProfile",             			// 任务名称 (用于调试)
        sizeof(iprofile_stack) / sizeof(StackType_t), // 栈大小 (以StackType_t为单位)
        NULL,                       		// 传递给任务的参数
        TD_PRI_LOW,                         // 任务优先级
        iprofile_stack,             		// 静态栈缓冲区
        &iprofile_thread_tcb            	// 静态任务控制块
    );
	if(iprofile_thread == NULL)
	{
		//Error_Handler();
        while(1);
	}

	test_sys_buf[8] = iprofile_thread;
	
	iProfile.sys_status.Group = 0;
	iProfile.temperature.new_inster_flag = 0;
  	
//	BleComModuleInit();
//	Blecomm_command_register(CMD_SET_Device_Reset,cb_reset_device);
//	Blecomm_command_register(CMD_GET_DeviceID,cb_get_device_id);
//	
//	Blecomm_command_register(CMD_GET_DeviceConf,cb_get_dev_conf);
//	Blecomm_command_register(CMD_SET_DeviceConf,cb_set_dev_conf);	
//	Blecomm_command_register(CMD_DeviceMem_opera,cb_dev_conf_opera);
//	Blecomm_command_register(CMD_GET_ProcessData,cb_get_process_data);
//	Blecomm_command_register(CMD_SET_DateTime,cb_SetDateTime);
//	Blecomm_command_register(CMD_GET_MeasParams,cb_GetMeasParameter);
//	Blecomm_command_register(CMD_SET_MeasParams,cb_SetMeasParameter);
//	Blecomm_command_register(CMD_GET_GroupFileList,cb_GetGroupFileList);
//	Blecomm_command_register(CMD_GET_GroupFileStream,cb_GetGroupFileStream);	
//	Blecomm_command_register(CMD_GET_FileStreamFast,cb_FileStreamFastRead);	
//	Blecomm_command_register(CMD_CTL_MeasControl,cb_MeasControl);
//	Blecomm_command_register(CMD_GET_MeasMemory,cb_GetMemory);
//	Blecomm_command_register(CMD_DEL_GroupFile,cb_DelGroupFile);
//	Blecomm_command_register(CMD_SET_Update,cb_upload_fwfile);
//	Blecomm_command_register(CMD_SET_RestoreFactory,cb_restore_factory);
//	Blecomm_command_register(CMD_GET_FlashCapacity,cb_get_flash_capacity);
//	Blecomm_command_register(CMD_OP_Calib,cb_op_thermo_calib);
//	Blecomm_command_register(CMD_GET_raw_data,cb_get_raw_data);    
//    Blecomm_command_register(CMD_LOG_Opera,cb_log_opera);
//	
//	BLECommunication_Start();
	
	USBComModuleInit();
	USB_comm_command_register(CMD_SET_Device_Reset,cb_reset_device);
	USB_comm_command_register(CMD_GET_DeviceID,cb_get_device_id);
	USB_comm_command_register(CMD_GET_WIFI_FIRMWARE,cb_get_WIFI_Firmware);	
	
	USB_comm_command_register(CMD_GET_DeviceConf,cb_get_dev_conf);
	USB_comm_command_register(CMD_SET_DeviceConf,cb_set_dev_conf);
	USB_comm_command_register(CMD_DeviceMem_opera,cb_dev_conf_opera);
	USB_comm_command_register(CMD_GET_ProcessData,cb_get_process_data);
	USB_comm_command_register(CMD_SET_DateTime,cb_SetDateTime);
	USB_comm_command_register(CMD_GET_MeasParams,cb_GetMeasParameter);
	USB_comm_command_register(CMD_SET_MeasParams,cb_SetMeasParameter);
	USB_comm_command_register(CMD_GET_GroupFileList,cb_GetGroupFileList);
	USB_comm_command_register(CMD_GET_GroupFileStream,cb_GetGroupFileStream);
	//USB_comm_command_register(CMD_GET_FileStreamFast,cb_FileStreamFastRead);	
	USB_comm_command_register(CMD_CTL_MeasControl,cb_MeasControl);
	USB_comm_command_register(CMD_DEL_GroupFile,cb_DelGroupFile);
	USB_comm_command_register(CMD_GET_MeasMemory,cb_GetMemory);
	USB_comm_command_register(CMD_SET_Update,cb_upload_fwfile);
	USB_comm_command_register(CMD_SET_RestoreFactory,cb_restore_factory);
	USB_comm_command_register(CMD_GET_FlashCapacity,cb_get_flash_capacity);
	USB_comm_command_register(CMD_OP_Calib,cb_op_thermo_calib);
	USB_comm_command_register(CMD_GET_raw_data,cb_get_raw_data);  
    USB_comm_command_register(CMD_LOG_Opera,cb_log_opera);
	// USB_comm_command_register(CMD_GET_WIFI_UPGRADE,WIFI_Upgrade);
		
	
	USBCommunication_Start();
	
#if WIFI_ENABLE == 1
	WIFIComModuleInit();
	WIFI_comm_command_register(CMD_SET_Device_Reset,cb_reset_device);
	WIFI_comm_command_register(CMD_GET_DeviceID,cb_get_device_id);
	WIFI_comm_command_register(CMD_GET_WIFI_FIRMWARE,cb_get_WIFI_Firmware);	
	
	WIFI_comm_command_register(CMD_GET_DeviceConf,cb_get_dev_conf);
	WIFI_comm_command_register(CMD_SET_DeviceConf,cb_set_dev_conf);
	WIFI_comm_command_register(CMD_DeviceMem_opera,cb_dev_conf_opera);
	WIFI_comm_command_register(CMD_GET_ProcessData,cb_get_process_data);
	WIFI_comm_command_register(CMD_SET_DateTime,cb_SetDateTime);
	WIFI_comm_command_register(CMD_GET_MeasParams,cb_GetMeasParameter);
	WIFI_comm_command_register(CMD_SET_MeasParams,cb_SetMeasParameter);
	WIFI_comm_command_register(CMD_GET_GroupFileList,cb_GetGroupFileList);
	WIFI_comm_command_register(CMD_GET_GroupFileStream,cb_GetGroupFileStream);
	WIFI_comm_command_register(CMD_GET_GroupBigFileStream,cb_GetGroupBigFileStream);
	//WIFI_comm_command_register(CMD_GET_FileStreamFast,cb_FileStreamFastRead);	
	WIFI_comm_command_register(CMD_CTL_MeasControl,cb_MeasControl);
	WIFI_comm_command_register(CMD_DEL_GroupFile,cb_DelGroupFile);
	WIFI_comm_command_register(CMD_GET_MeasMemory,cb_GetMemory);
	WIFI_comm_command_register(CMD_SET_Update,cb_upload_fwfile);
	WIFI_comm_command_register(CMD_SET_RestoreFactory,cb_restore_factory);
	WIFI_comm_command_register(CMD_GET_FlashCapacity,cb_get_flash_capacity);
	WIFI_comm_command_register(CMD_OP_Calib,cb_op_thermo_calib);
	WIFI_comm_command_register(CMD_GET_raw_data,cb_get_raw_data);  
    WIFI_comm_command_register(CMD_LOG_Opera,cb_log_opera);
#endif
    
	UI_init();
	
	return 0;
}

TaskHandle_t test_sys_buf[16];
/**
 * @brief       程序入口
 * @param       无
 * @retval      无
 */
void app_main(void)
{
	esp_err_t ret;

    ret = nvs_flash_init();         /* 初始化NVS 存储WiFi数据分区*/

    if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND)
    {
        ESP_ERROR_CHECK(nvs_flash_erase());
        ESP_ERROR_CHECK(nvs_flash_init());
    }

    BSP_Init();
    board_pins_init();        /* 初始化引脚 */
    key_power_on();

    iProfile_main_start();
#if WIFI_ENABLE == 1
 	wifi_ap_init();          /* wifi模块初始化*/
#endif	
   
    // led_init();                     /* 初始化LED */
    // uart1_init(115200);             /* 初始化串口1 */
    // timg_test();
    // W25Q64_test();
    // spiffs_test();
    // spi_flash_init();
    // wifi_ap_init();          /* wifi模块初始化*/
    // partition_init();               /* flash 分区初始化，读取数据 */
    // spiffs_init("storage", DEFAULT_MOUNT_POINT, DEFAULT_FD_NUM); /* 初始化文件系统*/
    // ret = spiffs_test_write();
    // if(ret != ESP_OK)
    // {
    //     ESP_LOGE("spiffs_test", "spiffs_test_write failed");
    // }
    
    ESP_LOGI("run", "Cond has run here");
    while(1)
    {
        vTaskDelay(pdMS_TO_TICKS(150));    /* 延时ms */
		//vTaskDelay(150);
    }
}
//




/********************************(C)COPYRIGHT 2016 RSEStudio ****************************/
/*****************************************END OF FILE************************************/


